I am working on a script that fetches csv files from a web server. I am using file_get_contents presently. Sometimes i get the message
Warning: file_get_contents failed to open stream: Connection timed out
I assume it can be due to website being down. Or can there be a situation where the website is fine but still this warning shows up. Also what advantage does CURL provide over this function.
this is because the remote url is having 404 error.
For accessing remote files, you should use cURL. You can set cURL to timeout quietly if the remote server takes too long.
Related
I'm trying to write the content of a page on my server to a variable using file_get_contents:
$lnk = "https://www.example.com/test%20file.php";
$otpt = file_get_contents($lnk);
The full URL is needed because I need the PHP output of the page and not the PHP script itself.
When running the above code I get this warning: failed to open stream: HTTP request failed! No other information, e. g. HTML error code, is provided. allow_url_fopen is enabled on the server. error_reporting(E_ALL) doesn't show any more information. The only thing which seems mentionable to me is that the file_get_contents request takes much too long (up to 30 secs) for the ~57 KB file I'm currently testing on.
I checked the Reference - What does this error mean in PHP?, but to no avail. I really have no idea what this message means since any further specification by PHP is missing. Any help would be very much appreciated.
Thanks in advance!
I am trying to create a simple script that will retrieve the last 5 feeds for a twitter user (in this instance the BBC)
It runs okay locally on my development server but once I upload this to a live site I get the following error:
Warning: file_get_contents(https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=bbc&count=5): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in ....
Does anyone know why this doesn't work on my live server (but fine on my dev server?)
As mentioned in file_get_contents throws 400 Bad Request error PHP, you may be better using curl instead of file_get_contents due to its improved error handling - this may provide you with another clue.
A little while ago I noticed some Soap errors emitting from my app and I started to investigate them. Stuff like:
SoapClient::SoapClient(http://###.###.###.###:8080/path/to/some.wsdl): failed to open stream: HTTP request failed!
SoapClient::SoapClient(): I/O warning : failed to load external entity "http://###.###.###.###:8080/path/to/some.wsdl"
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://###.###.###.###:8080/path/to/some.wsdl' : failed to load external entity "http://###.###.###.###:8080/path/to/some.wsdl"
It looked like a timeout on the remote server (WSDL caching was turned off). After bouncing that server and having no luck, I tried to just file_get-contents() the WSDL to see what would happen...
No dice: After about 20 seconds or so I got the same stream error:
file_get_contents(http://###.###.###.###:8080/path/to/some.wsdl) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed!
In a last ditch effort, I tried to read the contents via the curl_* functions, and I do in fact get what I'm looking for.
... tl;dr?
SoapClient and file_get_contents appear to be timing out (though not an explicit "Failed to open stream, connection timed out")
It appears to be related to streams since curl gives me what I'm looking for.
I've got a lot of code that depends on SoapClient and file_get_contents so switching to an all curl solution isn't really an option.
This is not a DNS issue as I can resolve external names fine (and my target resource is an IP)
allow_url_fopen is enabled.
Any ideas?
allow_url_fopen needs to be On in your PHP settings for file_get_contents to work with URLs. It'll give you that exact error otherwise. Double-check your PHP settings by loading a page with a phpinfo(); call to make sure they're not being overridden by a different php.ini or .htaccess file.
I'd guess a firewall problem otherwise but you say curl works from inside PHP, which would be opening sockets in the same manner.
I'm running a php in a unix shell and I'm getting the following error.
The script is a web scraper and it work fine on my host if I access it. But I want to run it as a cron job, What shoul I do?
Warning: file_get_contents(http://www.lomamatkat.fi/iframes/last-minute-offers/?lastminute_next=50): failed to open stream: no suitable wrapper could be found in /var/www/customers/lentovertailufi/public_html/matkat/script/lomamatkat.php on line 30
PHP Warning: file_get_contents(): URL file-access is disabled in the server configuration in /var/www/customers/lentovertailufi/public_html/matkat/script/lomamatkat.php on line 30
Update your php.ini file, and set the 'allow_url_fopen' option to 'On'.
If you are unable to change allow_url_fopen consider using use curl (if must be php) or Wget
go to the php.ini and enable that or use curl or one of the other http get functions. file_get_contents is considered a dangerous system call for urls because people can slip files into it as well.
I am using file_get_contents on my PHP and it throws some errors:
My code
#try to fetch from remote
$this->remotePath = "http://some-hostname.com/blah/blah.xml
$fileIn = #file_get_contents($this->remotePath);
The errors:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /virtual/path/to/file/outputFile.php on line 127
Warning: file_get_contents(https://some-host-name/data/inputFile.xml) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /virtual/path/to/file/outputFile.php on line 127
Any idea? It worked fine in my computer but stopped working when I ported it to the web server.
Your server must have the allow_url_fopen property set to true. Being on a free webhost explains it, as it's usually disabled to prevent abuse. If you paid for your hosting, get in contact with your host so they can enable it for you.
If changing that setting is not an option, then have a look at the cURL library.
It seems "allow_url_fopen" setting is false on your server and hence does not allow using URLs with file_get_contents().
Try using CURL instead that is a better and efficient way of communicating with other server.