0

ここでこのコードを使用しています:http ://www.digiways.com/articles/php/httpredirects/

    public function ReadHttpFile($strUrl, $iHttpRedirectMaxRecursiveCalls = 5)
    {
        // parsing the url getting web server name/IP, path and port.
        $url = parse_url($strUrl);
        // setting path to '/' if not present in $strUrl
        if (isset($url['path']) === false)
      $url['path'] = '/';
        // setting port to default HTTP server port 80
        if (isset($url['port']) === false)
     $url['port'] = 80;
        // connecting to the server]



        // reseting class data        
        $this->success = false;
        unset($this->strFile);
        unset($this->aHeaderLines);
        $this->strLocation = $strUrl;
  $fp = fsockopen ($url['host'], $url['port'], $errno, $errstr, 30);
        // Return if the socket was not open $this->success is set to false. 
        if (!$fp)
            return;
  $header = 'GET / HTTP/1.1\r\n';
  $header .= 'Host: '.$url['host'].$url['path'];
  if (isset($url['query']))
   $header .= '?'.$url['query'];
  $header .= '\r\n';
  $header .= 'Connection: Close\r\n\r\n';
  // sending the request to the server
  echo "Header is: <br />".str_replace('\n', '\n<br />', $header)."<br />";
  $length = strlen($header);
  if($length != fwrite($fp, $header, $length))
  {
   echo 'error writing to header, exiting<br />';
   return;
  }
  // $bHeader is set to true while we receive the HTTP header
  // and after the empty line (end of HTTP header) it's set to false.
  $bHeader = true;
  // continuing untill there's no more text to read from the socket
  while (!feof($fp))
  {
   echo "in loop";
   // reading a line of text from the socket
   //  not more than 8192 symbols. 
   $good = $strLine = fgets($fp, 128);
   if(!$good)
   {
    echo 'bad';
    return;
   }
   // removing trailing \n and \r characters.
   $strLine = ereg_replace('[\r\n]', '', $strLine);
   if ($bHeader == false) 
    $this->strFile .= $strLine.'\n';
   else
    $this->aHeaderLines[] = trim($strLine);
   if (strlen($strLine) == 0)
    $bHeader = false;
   echo "read: $strLine<br />";
   return;
  }
  echo "<br />after loop<br />";
  fclose ($fp);

    }

これが私が得るすべてです:

Header is:
GET / HTTP/1.1\r\n
Host: www.google.com/\r\n
Connection: Close\r\n\r\n
in loopbad

したがって、fgets($ fp、128);は失敗します。

4

2 に答える 2

1

欠陥はここにあります:

$good = $strLine = fgets($fp, 128);
if(!$good)
{
 echo 'bad';
 return;
}

fgets()成功した場合は文字列を返し、失敗した場合は FALSE を返します。ただし、返されるデータがこれ以上ない場合、fgets() は空の文字列 ( ) を返します''。したがって、両方とも$good$strLineの文字列に設定され、PHP はif()テストで FALSE に喜んでキャストします。次のように書き換える必要があります。

$strLine = fgets($fp, 128);
if ($strLine === FALSE) { // strict comparison - types and values must match
   echo 'bad';
   return;
}

$strLine直接テストできるので、二重代入は必要ありません。

于 2010-06-14T06:06:33.077 に答える
1

fopen を使用してリモート ファイルをフェッチする、デフォルトで有効になっている PHP の組み込み機能を使用していない理由はありますか?

$remote_page = file_get_contents('http://www.google.com/'); // <- Works!

難しく考えずにヘッダーのフェッチなどを行う必要がある場合は、高品質のサードパーティ製ライブラリもたくさんあります。サイズについては、Zend_Http_Clientをオンにしてみてください。

于 2010-06-14T05:30:34.800 に答える