I am trying to creating websocket with php and here is my connection that i used for create socket connection

<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

//Connect socket to remote server
if(!socket_connect($sock , '173.194.40.18' , 80))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not connect: [$errorcode] $errormsg \n");
}

echo "Connection established \n";

$message = "GET / HTTP/1.1\r\n\r\n";

//Send the message to the server
if( ! socket_send ( $sock , $message , strlen($message) , 0))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not send data: [$errorcode] $errormsg \n");
}

echo "Message send successfully \n";

//Now receive reply from server
if(socket_recv ( $sock , $buf , 2045 , MSG_WAITALL ) === FALSE)
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not receive data: [$errorcode] $errormsg \n");
}

//print the received message
echo $buf;
socket_close($sock);

This code i tried in browser and got only this

Socket created Connection established Message send successfully
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\WebsocketTests\phpwebsocket.php on line 38

and in terminal i got this

php phpwebsocket.php

Socket created
Connection established
Message send successfully
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.it/?gfe_rd=cr&ei=0011VfCvN8XD8geqj4D4BA
Content-Length: 258
Date: Mon, 08 Jun 2015 08:09:55 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p=0

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.it/?gfe_rd=cr&amp;ei=0011VfCvN8XD8geqj4D4BA">here</A>
.
</BODY></HTML>

somebody can tell me why i am getting different result in browser and cmd shell ?

The reason why you can't open a socket on port 80 while executing the page on port 80 is that there will be multiple instances of port 80 and this will conflict. Try executing the sockets on a port that your browser does not use although it would be very difficult.

In addition if this is not the case and you did not have a straight forward php installation then you could have php installed twice on the same computer like I've done myself then had to fix up. To check this make a script with <?php phpinfo(); ?> then compare it with the command line which I believe is php -version or php -v but may or may not have 2 dashes. If they are the same then you should be right on that aspect.

Try this lines of code -

<?php
    //Set time to 5 minutes
    ini_set('max_execution_time', 300);
?>

I hope that you don't mean an actual WS / WebSocket because if so you have got it all wrong. If on the other hand you are just trying to create a socket connection through web then you are almost there (even thought you don't bind your socket in an IP and port in your server) . If this is the case and you got the results that you given then everything works fine. But again , if you are trying to create an actual WS / WebSocket server through PHP you have a long road to go , I could provide some things to read and some links to open source projects but to be honest all those are just the top of the iceberg.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.