file_get_contents() error - php

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.

Related

Error when accessing youtube api: 'Warning: file_get_contents() [function.file-get-contents]: URL > file-access is disabled...'

I was trying to follow this answer:
https://stackoverflow.com/a/3331372/1063287
So in my php document I had the variables below:
$json = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_id}?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->{'entry'}->{'title'};
$video_date = $json_data->{'entry'}->{'published'};
$video_duration = $json_data->{'entry'}->{'media:group'}->{'yt$duration'};
$video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$video_description = $json_data->{'entry'}->{'content'};
but that gave me the error:
Warning: file_get_contents() [function.file-get-contents]: URL
file-access is disabled in the server configuration in
/home/path/to//file.php on line 12
Warning:
file_get_contents(https://gdata.youtube.com/feeds/api/videos/xx-xxxxxxx?v=2&alt=json)
[function.file-get-contents]: failed to open stream: no suitable
wrapper could be found in /home/path/to/file.php on line 12
I have looked at the developers guide here:
https://developers.google.com/youtube/2.0/developers_guide_php
and can understand the feed and entry structure here:
https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries
and have found a few solutions (one involving curl) but they either haven't worked or I haven't known if they were the best method to use.
Can anyone please tell me if there is anything wrong in the above code?
This is most likely because you do not have OpenSSL installed on your server. Try installing it, and if it still doesn't work it's probably because you have HTTP access disabled in your php.ini (set allow_url_fopen to true)
The best thing I saw for that is the Zend_GData_Youtube in the Zend Framework. It doesn't require anything special but it's much easier than a normal cURL or JSON request!
See there: http://framework.zend.com/downloads/latest

PHP failed to open stream when accessing url through proxy

I'm running a php site on localhost and i'm getting the following error.
Warning: file_get_contents(http://www.engadget.com/rss.xml)
[function.file-get-contents]: failed to open stream: HTTP request
failed! in C:\Program Files\xampp\htdocs\infohut\rss_read_class.php on
line 26
but when i run the same hosted in a real server, it doesn't give any error.
Also note that my local pc is connecting through a proxy and when i tried from a different pc with a direct connection to internet, the problem is not there.
So my guess is it has something to do with the proxy.
I'm using xampp for windows installation with php 5.1.4 and Apache 2.2.2
I tried adding my proxy settings to php.ini as well using below
pfpro.proxyaddress
pfpro.proxyport
still couldn't figure is out. Please advise if i need to change any other settings or anything.
Thanks
You should enable allow_url_fopen in your php.ini

Identifying root cause of a timeout in PHP

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.

Getting strange error in PHP script running on unix

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.

Connection Timeout With File_get_contents()

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.

Categories