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

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

问题描述

我在 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!!

function getUrlContent($url) {
    fopen("cookies.txt", "w");
    $parts = parse_url($url);
    $host = $parts['host'];
    $ch = curl_init();
    $header = array('GET /1575051 HTTP/1.1',
        "Host: {$host}",
        '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);
    return $result;
}

$url = "http://adfoc.us/1575051";
$html = getUrlContent($url);

谢谢大家的指导.

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

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