【问题标题】:Failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in Linux server无法打开流:HTTP 请求失败! Linux 服务器中的 HTTP/1.1 500 内部服务器错误
【发布时间】:2014-01-30 07:03:43
【问题描述】:

我想从这里获取提要http://laimoon.com/feed/jobs?search=female,arts&locations=2 如果我直接在浏览器中浏览此页面,它会显示提要,如果我在 Windows 上的本地主机中使用此代码

$xml=simplexml_load_file("http://laimoon.com/feed/jobs?search=female,arts&locations=2"); 
print_r($file_contents); 
                  OR
$file_contents = file_get_contents('http://laimoon.com/feed/jobs?search=female,arts&locations=2')
print_r($xml);

Both works fine 

但是如果我在我的 linux live 服务器中使用这个代码,那么它会显示这个错误 file_get_contents()

 Warning: file_get_contents(http://laimoon.com/feed/jobs?search=female,arts&locations=2): failed to open stream :HTTP request failed! HTTP/1.1 500 Internal Server Error

simplexml_load_file()

的这个错误
Warning: simplexml_load_file(http://laimoon.com/feed/jobs?search=female,arts&locations=2): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

提前致谢:)

【问题讨论】:

  • 500 Server error 是服务器端的问题。你可能对此无能为力。但是,file_get_contents() URL 包装器并不像它可能的那样健壮。使用 curl() 可能对您有用。
  • 但是如果这个错误来自服务器端,那么为什么当我直接从浏览器请求该 URL 时它不会发生?
  • 正如我所说 - 包装器不是那么好。有时服务器会因为不喜欢请求而返回 500 错误。我也遇到过类似的麻烦。我发现 curl() 通常更可靠。这只是一个建议。
  • 好的,我正在尝试通过 Curl 使用它
  • 即使我使用 CURL 也会再次出现 500 错误

标签: linux file-get-contents internal-server-error


【解决方案1】:

最后我得到了我的预期结果,我只需在 curl 链接中启用 curl_setopt($ch, CURLOPT_POST, true); 并得到我的结果,但现在还有一个问题已经解决了...... 在此之后,我使用 simplexml_load_string($ret); 加载该 xml 数据

但它没有读取 CDATA 然后我只添加了两个参数

 simplexml_load_string($ret,'SimpleXMLElement', LIBXML_NOCDATA);

然后我得到了我的预期结果...... 这是我的完整代码

$header = array('Content-Type:application/xml', 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201");
curl_setopt($ch, CURLOPT_URL, "http://laimoon.com/feed/jobs?search=female,arts&locations=2");
curl_setopt($ch, CURLOPT_POSTFIELDS, $loginFields);
$ret = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($ret,'SimpleXMLElement', LIBXML_NOCDATA);

$chl = $xml->children()->children();
$items = $chl->item;
foreach($items as $item){
  echo (string)$item->title."</br>";
  echo (string)$item->description."</br>";
  echo (string)$item->pubDate."</br>";
  echo (string)$item->link."</br>";

}

感谢@Mike W

【讨论】:

    【解决方案2】:

    未设置用户代理,在 php.ini 中设置 user_agent 或通过以下命令设置:

    ini_set('user_agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36');

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      相关资源
      最近更新 更多