64

My code is like this:

if( ($this->master=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0 )

    {
    die("socket_create() failed, reason: ".socket_strerror($this->master));
    }

when I run my php file which create connection with web socket, I got error Fatal error: Call to undefined function socket_create() in the line if( ($this->master=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0 )

Therefore i am not able to connect web socket and also not able to get response message from socket because Socket Status: 3 (Closed) is displaying in my browser when run client page .

Have you got an idea what is missing? Thank you in advance.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Sanjeev Kumar Jha
  • 1,213
  • 2
  • 12
  • 20

7 Answers7

72

You'll need to install (or enable) the Socket PHP extension: http://www.php.net/manual/en/sockets.installation.php

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • 2
    I just had to uncomment it in my php.ini; I also had to copy it from another directory to the PHP directory, but it came with the installation. – Jane Panda Jul 20 '12 at 13:16
  • Thanks Bob. i am using wamp server. First i changed in apache's php.ini and no effect. Then, i also changed in php's php.ini. At the end, websocket server started. :) – balamurugan Oct 04 '14 at 10:19
  • I have enabled `socket` extension in both php.ini but it still giving me same error. Do you guys have any idea why this would happen? – karmicdice Feb 05 '16 at 12:36
39

For a typical XAMPP install on windows you probably have the php_sockets.dll in your C:\xampp\php\ext directory. All you got to do is go to php.ini in the C:\xampp\php directory and change the ;extension=php_sockets.dll to extension=php_sockets.dll.

ambassallo
  • 924
  • 1
  • 12
  • 27
  • 1
    The only php.ini in my directory was php.ini-production and php.ini-development, and I could not find ;extension=php_sockets.dll anywhere in those files, so I jjust wrote the line extension=php_sockets.dll in both and saved. I'm still getting the undefined function socket_create() error though, any idea? I'm using windows. – 2316354654 Jun 15 '18 at 13:59
  • @2316354654 make sure your php.ini contains this lines`extension_dir="C:\xampp\php\ext" ` – Shashank Mishra Jun 21 '18 at 10:51
  • in my case `;extension=php_sockets.dll` was missing. I added it and now its working – log0 Jul 06 '20 at 06:42
11

If you are using xampp 7.3.9. socket already installed. You can check xampp\php\ext and you will get the php_socket.dll. if you get it go to your xampp control panel open php.ini file and remove (;) from extension=sockets.

Arman H
  • 1,594
  • 18
  • 22
5

Open the php.ini file in your server environment and remove ; from ;extension=sockets. if it's doesn't work, you have to download the socket extension for PHP and put it into ext directory in the php installation path. Restart your http server and everything should work.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
1

Follow these steps if you're on openSuse or SUSE.

Install php7 if it's not already installed.

zypper in php7

If you have php7 installed, update it with:

zypper update php7

Install php7-sockets

zypper in php7-sockets
0

Sometimes the version of your xampp (wamp or ...) is different with CLI. I suggest first using php --ini to find that use a correct version.

Moreover to solve this problem, you can open your xampp and use shell.

Mohamad Shabihi
  • 121
  • 1
  • 4
-2

I got this error when my .env file was not set up properly. Make sure you have a .env file with valid database login credentials.

Connor Leech
  • 18,052
  • 30
  • 105
  • 150
  • this looks like a solution for at specific situation, and not a general solution. .env files are not necessarily a standard for a php application... – JoSSte Jan 29 '22 at 20:36