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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Amir Tugi
  • 2,386
  • 3
  • 16
  • 18
  • 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. Jul 19 '16 at 11:51

3 Answers3

35

original answer moved to this topic .

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 4
    Why curl is "Better way"? Are these reasons absolute or relative-to requirements? thanks :) – Katapofatico Dec 03 '14 at 08:27
  • 1
    excuse me: There are reasons: http://stackoverflow.com/questions/5844299/using-file-get-contents-or-curl http://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents – Katapofatico Dec 03 '14 at 08: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 Dec 05 '14 at 20:14
  • You sir have saved me today, thank you for thy good deed ! – 3xCh1_23 Jan 29 '15 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 Jul 14 '16 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;
}
Ruel
  • 15,438
  • 7
  • 38
  • 49
3

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

Roman
  • 5,888
  • 26
  • 47
  • 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 Jul 06 '12 at 13:52