无法打开流:HTTP请求失败! Linux服务器中的HTTP / 1.1 500内部服务器错误

时间:2014-01-30 07:03:44

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

我想从这里获取Feed http://laimoon.com/feed/jobs?search=female,arts&locations=2 如果我在浏览器中直接浏览此页面,则显示feed,如果我在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 server中使用此代码,则会显示此错误  的的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

提前致谢:)

2 个答案:

答案 0 :(得分:0)

最后我得到了我的预期结果,我只是在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

答案 1 :(得分:0)

未设置用户代理,请在 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');