获取 url 内容 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11363022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:21:05  来源:igfitidea点击:

get url content PHP

phpurlcurlfile-get-contents

提问by Amir Tugi

I wanna put the content of a URL in a string and the process it. However, I have a problem.

我想将 URL 的内容放在一个字符串中并对其进行处理。但是,我有一个问题。

I get this error:

我收到此错误:

Warning: file_get_contents(http://www.findchips.com/avail?part=74ls244) [function.file-get-contents]: failed to open stream: Redirection limit reached,

I have heard this comes due to page protection and headers, cookies and stuff. How can I override it?

我听说这是由于页面保护和标题、cookies 和其他东西。我怎样才能覆盖它?

I also have tried alternatives such as fread along with fopen but I guess I just don't know how to do this.

我也尝试过 fread 和 fopen 等替代方案,但我想我只是不知道该怎么做。

Can anyone help me please?

有人可以帮我吗?

回答by T.Todua

original answer moved to this topic.

原始答案移至此主题

回答by Ruel

Use cURL,

使用cURL,

Check if you have it via phpinfo();

检查您是否通过 phpinfo();

And for the code:

对于代码:

function getHtml($url, $post = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    if(!empty($post)) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    } 
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

回答by Roman

Try using cURLinstead. cURL implements a cookie jar, while file_get_contents doesn't.

尝试改用cURL。cURL 实现了一个 cookie jar,而 file_get_contents 没有。