-2

I am trying to download the contents of an html file to my Ubuntu Linux 16.04 computer using php's get_file_contents() function. However, when I do this, I get this Warning: "failed to open stream: the , aborting"

Yet when I use wget on the terminal command line, it quickly downloads the file contents.

So why does file_get_contents not work for this? Here is my php code, which produces the Warning:

$testDownload = file_get_contents("https://ebird.org/region/US-AL-001?yr=all");

echo $testDownload;

On my Ubuntu terminal command line, here is my bash code, which works quickly and flawlessly:

wget https://ebird.org/region/US-AL-001?yr=all

I want to use php because I want to automate the downloading of a number of files and need a fair bit of code to do it, and I feel much more comfortable using php than bash.

P.S. I tried various "context" solutions for the file_get_contents function that were suggested on Stack Overflow, but they did not solve the problem.

P.P.S. I earlier tried cURL and got the same redirects Warning, though I admit to not knowing much about cURL.

user3409700
  • 637
  • 1
  • 5
  • 8
  • Maybe cou should give curl another try with the CURLOPT_MAXREDIRS option https://stackoverflow.com/a/31704183/1152471 – PKeidel Apr 09 '18 at 10:34
  • `print_r($http_response_header);` would enlighten you as to the reason. Bypassing a login scheme is too broad for this question/showcased prior effort. Consult with the hoster and their [TOS](https://ebird.org/science). – mario Apr 09 '18 at 10:44
  • I followed a suggestion on Stack Overflow to use the "context" option of file_get_contents to set max redirects to a higher number than default 20. I set it to 101. Still got the redirects warning. – user3409700 Apr 09 '18 at 11:07
  • Re: getting enlightenment via print_r($http_response_header), I did get some enlightening information from wget. It showed "302 Moved Temporarily Location: https://ebird.org/login/cas?portal=ebird [following]", which is a redirect. What I don't understand is why wget went ahead and successfully downloaded the file, whereas file_get_contents was stymied. – user3409700 Apr 09 '18 at 11:13

1 Answers1

0

I found a solution: the shell_exec() function in php allows me to use bash's wget command line function within my php script. I tried it and it worked. (I will have to change the ownership of the downloaded files to get access to them.) Here is the code that worked:

$output = shell_exec('wget https://ebird.org/region/US-AL-005?yr=all');

I still don't understand why wget can get the file contents but file_get_contents cannot. But with shell_exec() I have found a php solution to complete my task, so I am happy.

user3409700
  • 637
  • 1
  • 5
  • 8