fsocketopen not returning data properly

From: Date: Mon, 18 Jun 2007 23:49:19 +0000
Subject: fsocketopen not returning data properly
Groups: php.general 
Request: Send a blank email to php-general+get-257354@lists.php.net to get a copy of this message
I'm having trouble with sockets in PHP, if anyone has the time to help out that would be awesome. I am trying to get the result of a post echo httpSocketConnection("www.google.com", "POST", "/search", "q=test", "80"); This should post to google.com with a query of test. Then give me the response. The function I'm using is below. Here's the function's code: function httpSocketConnection($host, $method, $path, $data, $port) {
       $method = strtoupper($method);
       if ($method == "GET")
       {
           $path.= '?'.$data;
       }
       $filePointer = fsockopen($host, $port, $errorNumber, $errorString);
       if (!$filePointer)
       {
           logEvent('debug', 'Failed opening http socket connection: '.$errorString.' ('.$errorNumber.')<br/>\n');
           return false;
       }
       $requestHeader = $method." ".$path."  HTTP/1.1\r\n";
       $requestHeader.= "Host: ".$host."\r\n";
       $requestHeader.= "User-Agent:      Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
       $requestHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";
       if ($method == "POST")
       {
           $requestHeader.= "Content-Length: ".strlen($data)."\r\n";
       }
       $requestHeader.= "Connection: close\r\n\r\n";
       if ($method == "POST")
       {
           $requestHeader.= $data;
       }
       fwrite($filePointer, $requestHeader);
       $responseHeader = '';
       $responseContent = '';
       do
       {
           $responseHeader.= fread($filePointer, 1);
       }
       while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));
       if (!strstr($responseHeader, "Transfer-Encoding: chunked"))
       {
           while (!feof($filePointer))
           {
               $responseContent.= fgets($filePointer, 128);
           }
       }
       else
       {
           while ($chunk_length = hexdec(fgets($filePointer)))
           {
               $responseContentChunk = '';
               logEventToTextFile('debug', $chunk_length);
               $read_length = 0;
               while ($read_length < $chunk_length)
               {
                   $responseContentChunk .= fread($filePointer, $chunk_length - $read_length);
                   $read_length = strlen($responseContentChunk);
               }
               $responseContent.= $responseContentChunk;
               fgets($filePointer);
           }
       }
       logEventToTextFile('debug', $responseContent);
       return chop($responseContent);
   } 


« previous php.general (#257354) next »