douwei9973 2015-05-15 22:10
浏览 44

curl和file_get_contents不适用于特定站点(尝试过的用户代理)

I wanted to get the content of a specific url using php, so I tried both file_get_content and curl, but both returned an empty string.

Below are the code I used for the two functions.

For curl,

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER  =>  true,
    CURLOPT_URL => 'http://www.jjwxc.net/',
    CURLOPT_USERAGENT   =>  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36',
    CURLOPT_REFERER => "http://google.com",
    CURLOPT_HEADER  =>  TRUE,
    CURLOPT_FOLLOWLOCATION  =>  TRUE
));
$page = h(curl_exec($curl));
curl_close($curl);

For file_get_content,

$context = stream_context_create([
    'http'  =>  [
        'user_agent'    =>  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36'
    ]
]);
$page = file_get_contents('http://www.jjwxc.net/', false, $context);

Both codes are able to produce results for other sites, such as www.google.com.

I was able to open the webpage through browser, get content through command line curl, and through python requests.

I would really appreciate it if you can help me figure out what is the problem here.

  • 写回答

0条回答 默认 最新

    报告相同问题?