22

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?

1
  • To everyone who answered: None of it is a solution to fixing redirection loops. Or showing how to configure PHP correctly to raise the limit.
    – Daniel W.
    Commented Jul 19, 2016 at 11:51

3 Answers 3

35

original answer moved to this topic .

5
  • 4
    Why curl is "Better way"? Are these reasons absolute or relative-to requirements? thanks :) Commented Dec 3, 2014 at 8:27
  • 1
    excuse me: There are reasons: stackoverflow.com/questions/5844299/… stackoverflow.com/questions/11064980/… Commented Dec 3, 2014 at 8:30
  • There's conflicting information in those above links: "FWIW there's little difference with regards to speed. I've just finished fetching 5,000 URLs and saving their HTML to files (about 200k per file). I did half with curl and half with file_get_contents as an experiment and there was no discernible difference." and follow-on comment "I benchmarked the two on 5.3 and 5.4, and cURL still is considerably faster than file_get_contents, especially for multiple calls on the same request." vs "A few years ago I benchmarked the two and CURL was faster."...
    – klidifia
    Commented Dec 5, 2014 at 20:14
  • You sir have saved me today, thank you for thy good deed !
    – 3xCh1_23
    Commented Jan 29, 2015 at 15:54
  • @tazo todua How can I get the html text, when I run this example it display the whole website view.
    – Merbin Joe
    Commented Jul 14, 2016 at 16:50
17

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;
}
2
  • What is this line for? curl_setopt($ch, CURLOPT_POST, true);
    – Amir Tugi
    Commented Jul 6, 2012 at 15:19
  • 1
    Nope, that doesn't do it
    – Amir Tugi
    Commented Jul 6, 2012 at 15:46
3

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

1
  • 3
    could you give me an example of how i get the content of a url with cURL? I have a problem with the example of php.net
    – Amir Tugi
    Commented Jul 6, 2012 at 13:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.