Failed HTTP request with file_get_contents without further specification - php

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!

Related

file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in json

getting a error on file_get_content it is replacing & to & hence i am getting d error. data which i am getting is in json format.
original url is: xyz.abc.com/index.php/DeviceDetails/GetDeviceHistory?tracking_id=70&start_time=2017-03-05 13:14:50&end_time=2017-03-08 13:14:50
<b>Warning</b>: file_get_contents(http://xyz.abc.com/index.php/DeviceDetails/GetDeviceHistory?tracking_id=70&start_time=2017-03-05 13:14:50&end_time=2017-03-08 13:14:50): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in
<b>/home/abc/public_html/php/service.php</b> on line
<b>1855</b>
<br />
{"Status":"false","message":"No data found"}
please help me with this error
Make sure you actual URL is starting with http:// or https:// and use quotes while calling file_get_contents
file_get_contents("http://your_URL_here")
Try running PHP html_entity_decode() on your URL before you pass it to file_get_contents(): http://php.net/manual/en/function.html-entity-decode.php (as Kuldeep Singh says above, be sure you are passing the URL as a string :D)
I have had many many many problems similar to this. It could be a number of things, a missing header, invalid user agent, bad php.ini config, etc.
I would highly recommend using a cURL Library that allows you to customize headers, port, etc. of the request so you can properly debug. This is the custom PHP cURL library I use: https://gist.github.com/schwindy/e5798405d0b6269945bdf037a58f6a4d
Hope some of that helps. Good luck.

PHP Error "file_get_contents & Authorization Required" in Wordpress

i got a problem with one of my wordpress plugins. When i activate it this message shows on top of the screen.
Warning: file_get_contents(http://gruender-news24.de/wp-content/plugins/ultproteam_/includes/../css/font-awesome.css): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /mnt/web7/e3/69/5941469/htdocs/WordPress_08/wp-content/plugins/ultproteam_/includes/plugin-class.php on line 336
This code is on line 336.
$pTsubject = file_get_contents( $this->mainPath . '/css/font-awesome.css');
Im not really familiar to php and i couldnt figure it out by myself.
This seems to offer a solution but im not able to "transform" it on my special problem.
http://www.hashbangcode.com/blog/using-authentication-and-filegetcontents
Thanks alot for your help!
file_get_contents() utilizes the fopen() wrappers, therefore it is restricted from accessing URLs through the allow_url_fopen option within php.ini
OR
try using CURL.
lots of good answers in what looks like a duplicate question here.

Downloading large external file in PHP - without error/timeout

I'm incredibly frustrated as I can't seem to get past this error, no matter how I try.
My website requires I download a large (zipped) XML product feed - daily. The file size is variable; I get PHP warnings when the product feed is larger. There MUST be a way to resolve this!
The script downloads the file (still zipped) to my server:
<?php
file_put_contents('datafeed.xml.gz', file_get_contents('http://external website.com/format/xml/compression/gzip/'));
Error: PHP Warning: file_get_contents(...): failed to open stream:
HTTP request failed! in... etc.
How do I solve this? Much appreciated!

Failed to open stream: HTTP request failed-Google API Request for calculating distance in php?

I am sending the request to google maps api services to calculate the distance like this url:
$apiURL = "http://maps.google.com/maps/geo?&output=xml&key=AIzaSyCYz5Kpw4xTYVwAUibaMIUVBcaL-RfTumk&q=myAddress"
I am trying to get the response using file_get_contents($apiURL), & it is working well with my Local system. When i uploaded the same file to the server, the output of file_get_contents($apiURL) is appearing to be null & giving error like -
Warning:
file_get_contents(http://maps.google.com/maps/geo?&output=xml&key=AIzaSyCYz5Kpw4xTYVwAUibaMIUVBcaL-RfTumk&q=Aerekere
%2Cbangalore%2Ckarnataka) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor2-wc1-dfw1/410304/hfg.mpadusa.com/web/content/components/com_helping/controllers/distance.php
on line 108.
I have checked couple of things like
allow_url_fopen : On it's ok
New ApI key to the Linux server.
In local windows system, all response is ok.
What might me the issue. Any help is really appreciated.
file_get_contents is often disabled/blocked on web servers due to security risks when accessing external content (e.g. through http).
As a matter of good practice, always use curl functions to get external content, including calls to the google API. You can find plenty of examples of using curl on the web, but a simple place to start is the manual: http://au2.php.net/curl_exec

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