PHP fopen() fails to open url resource - php

I'd like to fopen this url: http://ec.europa.eu/eurostat/SDMX/diss-web/rest/data/hlth_cd_asdr/A..T.TOTAL.A-R_V-Y./startperiod=2010&endPeriod=2010
like this:
$stream=fopen('http://ec.europa.eu/eurostat/SDMX/diss-web/rest/data/hlth_cd_asdr/A..T.TOTAL.A-R_V-Y./startperiod=2010&endPeriod=2010','r');
but it fails with the following warning:
failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
What should I do to open this link in PHP?

fopen() wouldn't support http requests so you can use file_get_content() in your scanario. For more detail please read fopen() and file_get_contents() from php.net.

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 file_get_contents sometimes failed to open stream Http request failed

I have an ajax call that gets many, many pages of contents from a website, but it fails to open steams sometimes even though the same exact links that failed to open streams works just fine, when I ran them again. The problem is, then other links would failed to open stream. Anyone know why this is happening?
Warning: file_get_contents(http://www.merriam-webster.com/browse/dictionary/a/acoustically.htm): failed to open stream: HTTP request failed!

Why doesn't file_get_contents() appear to have the same access as curl_exec()?

I am downloading favicons using PHP and I spent a while troubleshooting before I noticed that curl_exec appears to work for some sites while file_get_contents does not.
For example here is the response from http://www.hellmanns.com/favicon.ico
file_get_contents
Warning: file_get_contents(http://hellmanns.com/favicon.ico): failed
to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in
/home/arcmarks/public_html/arcmarks/source/class.FaviconFinder.php on
line 44
curl_exec
h(
e?Câ¬^ÏÉÌŒ$ B#(ÌlcYfƒy9C¥D;üès¨H8§¥§‹&5é¨QÛœJ©O5Öžfå§LíáÛÜ߸n Ü®Œa~ †}…l
“‘±“o}þþþ¥FHòòÞïïï/Á±mžç¿~L"-‚y¾i'öá̳²¶£A%«§¬?ùíÜ©„i­²~Âh*®^?ÖáµÅÅ™ØezU[¢<4¡-5;/ïÀöæØaéìÐ̆cN
,;U#N㽧õðî‰Õ¨îѹk_jÖšmîÙÌáÜÝ[æ™-ì·D¡?-s­wE‹# õ¹„$0”6&ï²1Ú¦Žž#”+$§HR¢#3€,3Â#üÿÿþÿÿP:H€ô¯VüáBâ™?„j
W~±¦ª†ôòóaQ ŸE7Ç|#F2?G2?åóãäå¸~Q
p,KEv(Cud).#lUH4mkr$gj[ahVf9&2Se/#ZLwPYiF0qOG?y>QbJx+cIMDzW!-]*'t5B;<^%T6
_XsR=` :n"A7831o N ÿÿÿÿÿÿÿÿ€Àÿÿÿÿÿÿÿÿ
curl_exec is based upon and entire project that is used in many different languages see here:
Why doesn't file_get_contents() appear to have the same access as curl_exec()?
Hence it has much more support.
The 403response error indicates that the server rejected the request made by file_get_contents.
Curl and file_get_contents do not make the same HTTP request.
At a minimum CURL will use a different user-agent Header.
In general CURL is superior and faster than using file_get_contents for loading remote files.

use get parameters in DOMDocument::loadHTMLFile()

I want to load an url like this using DOMDocument::loadHTMLFile(): http://www.domain.com/dir/page?param1=235235&param2=2345, but it gives this error, even though the page exists:
Warning: DOMDocument::loadHTMLFile(http://www.domain.com/dir/page?param1=235235&param2=2345): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
How can I solve this?

I'm having trouble getting file_get_contents() to work on my server

I've got a php file on my server that looks like this:
echo file_get_contents('http://server.com/some.file');
and I get the following error:
Warning: file_get_contents(http://server.com/some.file) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /path/to/php/file on line 6
What's got me confused is that if I link to the/full/server/path instead of http:// it seems to load fine. Also, I can get it to load content from other domains using http://, just not my own.
Any ideas?
As deceze sayd test the path in a browser. It should work the same as in PHP. If the path you write is correct check your URL rewrites (maybe you have them different for the server IP).

Categories