How to run PHP sockets in WAMP SERVER

Lately I've been playing around with HTML5 Web Sockets. This has been pretty fun and educational. Especially since I've ran into a few problems, and very little documentation is available on it. After resisting the temptation of pulling my hair out (since I don't have a whole lot of it left, anyways), and focusing on finding answers to these problems, I was rewarded by a generous universe, who revealed me all the answers I needed. This post will cover 3 ways to help you solve the common error you may run into when attempting to create sockets in PHP using WAMP (Windows, Apache, MySQL, and PHP). This error message says Call to undefined function socket_create().

How to fix Call to undefined function socket_create()
If you're running WAMP, there are two basic ways to fix this. This error is caused by a simple WAMP/PHP configuration that you left out. What you want to do is pretty simple. Simply enable sockets in PHP. To do so, you must:
1. Click the WAMP icon
2. Select PHP

3. Select PHP Extensions
4. Enable the php_sockets extension

Now try restarting all services in your WAMP, and see if the sockets work now. This should solve the problem 9/10 times. However, there are some situations when you will do just what I described above, and your PHP sockets will still return that undefined function socket_create(), which will be frustrating. However, here are some other things that will fix this problem:


Set up your environment variables to run PHP from the command line
A few weeks ago I posted a tutorial on how to run Python from the command line, where I describe how to set up the environment variables in Python. The exact same process is used for PHP and WAMP, so you can use that as a reference. The steps are simple:
Click the WAMP icon
Select www directory
This will take you to the directory where WAMP is installed. Go up one directory level from the www folder, and select the bin directory. Inside bin you will see a PHP directory, inside of which there will be a PHP5.3.4 (whatever version of PHP you happen to have installed). Follow this directory path until you find the one that has a file named php.exe
Copy the complete path to this directory
In your environment variables window (follow the Python article on the specifics on how to get there), edit the PATH variable, and paste this path to your PHP.exe (the PHP interpreter) file that you copied in the previous step

Now you should be able to go do a DOS window and type in a command like:

php file.php

(assuming that you're in a directory that has a file named file.php, of course)

This will execute your script right in the command window. If you're trying to run a PHP socket server so clients can connect to it, you will need to run it from a terminal (command line), and this is how you set it up.

However, there are 2 more ways to solve the socket problem ("undefined function socket_create()"). They're both pretty similar, although the second one is less obvious.

Method #1: Edit your php.ini file
This basically accomplishes the exact same thing you did in the fist thing I suggested earlier in this PHP tutorial. What you need to do is:
Click the WAMP icon
Select PHP
Select php.ini
Search your php.ini file for the following line:
;extension=php_sockets.dll

This line has a semi-column at the beginning, meaning that the sockets extension is disabled. To fix this, simply uncomment the statement, and save the php.ini file. To uncomment this line of code, simply delete the semi-column, changing the line to this:
extension=php_sockets.dll


Again, after restarting all WAMP services, try running your PHP script with the create_socket() function call, and things should work out just fine now.

Method #2: Edit the other php.ini file
Sometimes your WAMP will install weird, and the PHP will point to a different php.ini file than the one you can access through the WAMP icon. I'm not sure how this can happen, but without using the technique I'll show here, the problem with the sockets will never go away. I came across this problem when I installed WAMP in a virtual machine running Ubuntu (the virtual machine had Windows XP installed in it). To find out what php.ini your PHP is looking at, go to a command line and type in the following PHP command:
php --ini

This will list the information (file paths) about the configuration of your PHP that you need to edit. The second line of the output you get from this step shows where the true php.ini file is located. Go to that path and uncomment the line that points to the sockets.dll file as described in the previous step.

The output from the php --ini command will look something like this:
C:\Users\Rodrixar>php --ini

Configuration File (php.ini) Path: C:\Windoows

Loaded Configuration File: C:\wamp\bin\php\php5.3.4\php.ini

Scan for additional .ini files in: (none)

Additional .ini files parsed: (none)

Notice how in this case the .ini file is located within my WAMP folder, which is why the other steps shown in this tutorial worked. But some times this location will be different from the WAMP install location, and by changing that php.ini file you will be able to truly enable and use those desired PHP sockets.