Exemplo n.º 1
0
 static function fsockopen_request($url, $r)
 {
     $arrURL = parse_url($url);
     $fsockopen_host = $arrURL['host'];
     if (!isset($arrURL['port'])) {
         $arrURL['port'] = 80;
     }
     $arrURL += array('path' => '');
     //prevent notice
     //fsockopen has issues with 'localhost' with IPv6 with certain versions of PHP, It attempts to connect to ::1,
     // which fails when the server is not setup for it. For compatibility, always connect to the IPv4 address.
     if ('localhost' == strtolower($fsockopen_host)) {
         $fsockopen_host = '127.0.0.1';
     }
     $iError = null;
     // Store error number
     $strError = null;
     // Store error string
     $handle = @fsockopen($fsockopen_host, $arrURL['port'], $iError, $strError, $r['timeout']);
     if ($handle === false) {
         return false;
     }
     gpRemoteGet::stream_timeout($handle, $r['timeout']);
     $requestPath = $arrURL['path'] . (isset($arrURL['query']) ? '?' . $arrURL['query'] : '');
     if (empty($requestPath)) {
         $requestPath .= '/';
     }
     $strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
     $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
     if (isset($r['user-agent'])) {
         $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
     }
     $strHeaders .= "\r\n";
     fwrite($handle, $strHeaders);
     $strResponse = '';
     while (!feof($handle)) {
         $strResponse .= fread($handle, 4096);
     }
     fclose($handle);
     $process = gpRemoteGet::processResponse($strResponse);
     $processedHeaders = gpRemoteGet::processHeaders($process['headers']);
     // If location is found, then assume redirect and redirect to location.
     if (isset($processedHeaders['headers']['location'])) {
         return gpRemoteGet::Redirect($processedHeaders, $r, $arrURL);
     }
     $strResponse = gpRemoteGet::chunkTransferDecode($strResponse, $processedHeaders);
     return array('headers' => $processedHeaders['headers'], 'body' => $process['body'], 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
 }