douzhang5121
douzhang5121
2012-05-09 22:03
浏览 370
已采纳

为什么我在使用file_get_contents()时遇到500错误,但在浏览器中有效?

$html = file_get_contents("https://www.[URL].com"); 
echo $html;

produces this in the error logs:

PHP Warning: file_get_contents(https://www.[URL].com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /Applications/MAMP/htdocs/test.php on line 13";

However, the site works fine in a browser.

I tried using cURL as well. I don't get any errors in the log file, but $html now echoes:

Server Error in '/' Application.
Object reference not set to an instance of an object.

...some more debugging info

Any ideas how to work around this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqiao3927
    dongqiao3927 2012-05-09 22:08
    已采纳

    Try this workaround:

    $opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0
    "));
    $context = stream_context_create($opts);
    $header = file_get_contents('https://www.example.com',false,$context);
    

    If this doesn't work, maybe you cant read from https?

    点赞 评论
  • dseomy1964
    dseomy1964 2015-05-19 13:05

    I had to enter more data into the header:

    $opts = array('http' => array(
        'method' => "GET",
        'header' => "User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
    "
        . "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    "
        . "Accept-Encoding:gzip, deflate
    "
        . "Accept-Language:cs,en-us;q=0.7,en;q=0.3
    "
        . "Connection:keep-alive
    "
        . "Host:your.domain.com
    "
        ));
    $context = stream_context_create($opts);
    $html = file_get_contents($sap_url, FALSE, $context);
    
    点赞 评论

相关推荐