file_get_contents不适用于某些网址 [英] file_get_contents is not working for some url

查看:107
本文介绍了file_get_contents不适用于某些网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中使用 file_get_contents 。在下面的代码中,第一个URL工作正常,但第二个不工作。

I use file_get_contents in PHP. In the below code in first URL works fine but the second one isn't working.


$URL = "http://test6473.blogspot.com";
$domain = file_get_contents($URL);
print_r($domain);


$add_url= "http://adfoc.us/1575051";
$add_domain = file_get_contents($add_url);
echo $add_domain;

有关为什么第二个不起作用的任何建议?

Any suggestions on why the second one doesn't work?

推荐答案

由file_get_contents检索的URL,因为它们的服务器检查请求是来自浏览器还是任何脚本。如果他们从脚本中发现请求,他们只是禁用页面内容。

URL which is not retrieved by file_get_contents, because their server checks whether the request come from browser or any script. If they found request from script they simply disable page contents.

所以我必须提出一个类似于浏览器请求的请求。所以我使用下面的代码来获取第二个url内容。它可能不同于不同的Web服务器。因为他们可能会保留不同的检查。

So that I have to make a request similar as browser request. So I have used following code to get 2nd url contents. It might be different for different web server. Because they might keep different checks.

即使为什么不尝试使用以下代码!如果你幸运,这可能为你工作!

Even though why dont you try to use following code! If you are lucky this might work for you!!



fopen("cookies.txt", "w");
$url="http://adfoc.us/1575051";
$ch = curl_init();

$header=array('GET /1575051 HTTP/1.1',
    'Host: adfoc.us',
    'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language:en-US,en;q=0.8',
    'Cache-Control:max-age=0',
    'Connection:keep-alive',
    'Host:adfoc.us',
    'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
    );

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
    curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

    curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
    curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    $result=curl_exec($ch);

    curl_close($ch);

感谢大家指导我。

这篇关于file_get_contents不适用于某些网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆