get url content PHP

2019-01-07 15:28发布

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

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?

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

Can anyone help me please?

3条回答
Animai°情兽
2楼-- · 2019-01-07 15:35

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

查看更多
Anthone
3楼-- · 2019-01-07 15:49

1) local simplest methods

<?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) Better Way is CURL:

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

See function code here. It automatically handles FOLLOWLOCATION problem + Remote urls are automatically re-corrected! ( src="./imageblabla.png" --------> src="http://example.com/path/imageblabla.png" )


p.s.GNU/Linux users might need php5-curl package.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-07 15:52

Use cURL,

Check if you have it via 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;
}
查看更多
登录 后发表回答