dogadikbayir
dogadikbayir

Reputation: 51

Cannot receive data from socket using PHP

I'm new to php and I need to receive data from a socket in order to parse the song title from a given IP. In order to learn and test sockets I first tried to connect and receive data from the given IP. Here's my code:

     #!/usr/bin/php -q
     <?php
    //$sock = fsockopen('205.164.35.5:80');
    $sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    $result = socket_connect($sock, "205.164.35.5", 80);

    $request = "GET / HTTP/1.1\r\n";
    $request .= "Icy-MetaData: 1\r\n";

    socket_write($sock,$request,strlen($request));
    echo "OK";

    $out = " ";
    while($out = socket_read($sock,2048)){

    echo $out;
    }

    socket_close($sock);
      ?>

When I run it from the terminal it does not generate any errors, however it displays nothing. I tried to connect to that ip with "telnet" command and sent the same request and on the terminal I had a response. Any kind of help would be really appreciated.

Thank you...

Upvotes: 0

Views: 117

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

You should terminate the request with two CRLFs:

- $request .= "Icy-MetaData: 1\r\n";
+ $request .= "Icy-MetaData: 1\r\n\r\n";

Hope it helps.

Upvotes: 2

Related Questions