获取URL内容PHP [复制]获取URL内容PHP [复制](get url content PHP

2019-06-14 16:09发布

这个问题已经在这里有一个答案:

  • 获得从URL文件内容? 5个回答

我想把一个URL的内容在一个字符串和处理它。 但是,我有一个问题。

我得到这个错误:

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

我听说过这个来由于网页保护和头,饼干之类的东西。 我怎么可以重写呢?

我也试图替代品,如用的fread沿的fopen,但我想我只是不知道如何做到这一点。

任何人都可以帮助我吗?

Answer 1:

1) 本地最简单方法

<?php
echo readfile("http://example.com/");   //needs "Allow_url_include" enable
//OR
echo include("http://example.com/");    //needs "Allow_url_include" enabled
//OR
echo file_get_contents("http://example.com/");
//OR
echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb"  //needs "Allow_url_fopen" enabled
?> 

2) 更好的方法是卷曲

echo get_remote_data('http://example.com/?myPage', 'var2=something&var3=blabla' ); // GET & POST request

见这里的功能代码 。 它会自动处理FOLLOWLOCATION问题+远程URL会自动重新修正! ( src="./imageblabla.png" --------> src="http://example.com/path/imageblabla.png"


psGNU / Linux的用户可能需要php5-curl包。



Answer 2:

使用cURL

检查您是否通过有它phpinfo();

而对于代码:

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;
}


Answer 3:

尝试使用卷曲来代替。 卷曲实现了一个饼干罐,而的file_get_contents没有。



文章来源: get url content PHP [duplicate]