php failed to open stream http request failed
file_get_contents failed to open stream http request failed
I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get:
Warning: file_get_contents(http://. /cron.php?act=junoo): failed to open stream: HTTP request failed! in /home/. /. /. /userdat.php on line 37
The code I am using is:
2 Answers 2
You can use php curl lib to call API first and store the contents into a file. When you have the file, you can use your code with that file.
Try following the networking request in the networking tab of the developer tools in your browser. The browser may have changed the connection to use https:// or redirected to another link. It is also possible that the request is missing cookie data that the browser already has stored.
Not the answer you’re looking for? Browse other questions tagged php or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
failed to open stream: HTTP request failed! #2624
Comments
rahulpramodpande commented Jan 23, 2014
When execute composer update or composer install it says
«$ composer update
Loading composer repositories with package information
$ composer diag
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity: OK
Checking composer.json: OK
Checking disk free space: OK
Checking composer version: OK.
Getting irritate. Please help me out.
The text was updated successfully, but these errors were encountered:
We are unable to convert the task to an issue at this time. Please try again.
The issue was successfully created but we are unable to update the comment at this time.
lavoiesl commented Jan 24, 2014
robertkraig commented Jan 28, 2014
I’m having the same issue with silex right now.
robertkraig commented Jan 28, 2014
Loading composer repositories with package information
Installing dependencies (including require-dev)
[Composer\Downloader\TransportException]
The «http://packagist.org/p/silex/silex$47fabe945a95e671ee161f60b90d1b8eac2a3a953f851abb2140ddd4e3528496.json» file could not be downloaded: failed to open stream: HTTP request failed!
install [—prefer-source] [—prefer-dist] [—dry-run] [—dev] [—no-dev] [—no-plugins] [—no-custom-installers] [—no-scripts] [—no-progress] [-v|vv|vvv|—verbose] [-o|—optimize-autoloader]
lavoiesl commented Jan 28, 2014
It would be important to know if you can open that URL in a browser or using cURL when you are having this issue. Otherwise, there is no way of knowing if it is related to composer or if Packagist is simply not responding.
johnnygreen commented Jan 28, 2014
I’m getting the same errors for all repos when I try to composer update.
Also a wget to the same URL:
I can open the URL in my browser with no issues.
You can see from the cURL that two redirects are occurring, in which the last redirect results in a 404 not found.
Composer version 4ec4ba1 2014-01-27 13:36:06
lavoiesl commented Jan 28, 2014
johnnygreen commented Jan 28, 2014
Tried it again with quotes and got the same result. It still seems to convert it to bash.
stof commented Jan 28, 2014
@johnnygreen use single quotes. double quotes don’t prevent the bash replacement. They only prevent spaces to be considered as argument delimitors
lavoiesl commented Jan 28, 2014
johnnygreen commented Jan 28, 2014
Removed clockwork from the equation.
johnnygreen commented Jan 28, 2014
Single quotes during wget seem to work with the aws download.
wget ‘http://packagist.org/p/aws/aws-sdk-php$926152c0c12e923c27fee5e77572acf2fe491d621c387d3d9eb72cfc10e8a917.json’
—2014-01-28 14:44:00— http://packagist.org/p/aws/aws-sdk-php$926152c0c12e923c27fee5e77572acf2fe491d621c387d3d9eb72cfc10e8a917.json
Resolving packagist.org. 87.98.253.214
Connecting to packagist.org|87.98.253.214|:80. connected.
HTTP request sent, awaiting response. 200 OK
Length: unspecified [application/json]
Saving to: ‘aws-sdk-php$926152c0c12e923c27fee5e77572acf2fe491d621c387d3d9eb72cfc10e8a917.json’
65,552 199KB/s in 0.3s
So I guess that means that the wget is working, thus the internet connection isn’t the problem.
johnnygreen commented Jan 28, 2014
Welp, a simple restart of my machine seems to have resolved my issues.
nickistre commented Feb 4, 2014
I’ve hit this same issue when I was setting up a project using composer, As a quick example of attempting to use composer to setup a skeleton ZF2 application via http://framework.zend.com/downloads/skeleton-app
Rebootiing my system isn’t working. I am running xdebug for my CLI scripts (as I need to be able to debug cli scripts in a project), which seems to cause composer diag to fail:
Output of selfupdate:
[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
I am periodically getting this error, i.e it is only happening at random times, any ideas what would cause this, i have checked my php.ini and allow_url_fopen is set to true.
4 Answers 4
If the site requires basic authentication, you can give your credentials this way:
If it uses digest authentication, you’ll have to handle it manually by reading the headers of the failed response and sending a new one with the correct headers. See HTTP context options for how to read and set headers and see how digest works in HTTP authentication with PHP.
The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in «HTTP Authentication: Basic and Digest Access Authentication» [43].
You are trying to access a site that requires authentication, as maggie pointed out. fopen() does not support HTTP Basic Authentication, so you must use the Client URL Library to achieve such functionalities.
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
I am accessing images from another website. I am getting this Error:
«failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request » error when copying ‘some(not all)’ images. here is my code.
6 Answers 6
I think preg_replace make more better sense as it will work with latest versions of the PHP as ereg_replace didn’t worked for me being deprecated
I had the same problem, but it was solve by
Thanks Cello_Guy for the post.
The only issue I can think of is spaces being in the url, most likely in the file name. All spaces in a url need to be converted to their proper encoding, which is %20.
If you have a file name like this:
You would get the above error, but with this:
You should have to problems.
Just use the str_replace() to replace the spaces (» «) for their proper encoding («%20»)
It looks like this:
For more information on the str_replace() check out The PHP Manual.
Use the function rawurlencode()
Encodes the given string according to » RFC 3986.
Even a trailing blank in the url can cause php file($url) to fail. In recent versions of php or apache even a trailing blank in the url will cause the error. So the url appears to work in a browser because the browser knows enough to %20 the trailing blank or ignore it. That was my error anyway.
Older LAMP allowed it. (ie. same code ran ok). Easy fix.
It seems that the URL has some spaces or other special characters, you need to encode it, use urlencode() function
Not the answer you’re looking for? Browse other questions tagged http httpwebrequest or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.