且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP file_get_contents 对某些 URL 不起作用

更新时间:2023-02-23 21:27:17

就像@MarkBaker 在评论中所说的,webserver 可能正在阻止未知的 UserAgents,尝试伪造一个:

Like @MarkBaker said in the comment, webserver is probably blocking unknown UserAgents, try to fake one:

$context = stream_context_create([
    'http' => [
        'user_agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
    ],
]);
$html = file_get_contents('http://modagram.com', false, $context);

print_r($http_response_header); // see response headers