I am trying to get postal codes from this site:
http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce
My code is simple:
<?php
$postalCode = $_GET['code'];
$httpAddr = 'http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce/Okr%C4%99g_'.$postalCode[0].'_'.$postalCode[0].$postalCode[1].'-xxx';
file_get_contents($httpAddr);
?>
But when i set $postalCode to 03-000 (also 01-000, 05-000, but for 07-000, 61-000, 62-000 is working) i am reciving error:
Warning: file_get_contents(http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce/Okr%C4%99g_0_03-xxx): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /var/www/clients/client1/web4/web/ofix/test.php on line 5
Page address is correct, you can copy and past it in your web browser and it works.
Any ideas?
As Lightness Races in Orbit suspected, it does seem that the webserver is blocking PHP's request.
Using cURL instead of file_get_contents() reveals the details:
HTTP/1.0 403 Forbidden
Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
A web browser sends a valid User-Agent header in its request, which is why the page loads OK in your browser but not in PHP.
In my tests loading this URL in PHP, sometimes it succeeds with an HTTP status code of 200, other times it fails with 403. Notice that the error message says scripts may be blocked (ie. sometimes they may not be blocked).
Edit
See this question for more info: How to get results from the Wikipedia API with PHP?
Related
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.
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¡?-swE‹# õ¹„$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.
part of my website allows users to enter a URL and the image from that URL is copied and saved.
The other day i had this issue occur:
Warning: imagecreatefromjpeg(http://preview.netcarshow.com/Ford-Focus_RS500-2011-hd.jpg): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
Upon research, it was determined that this issue was because the given website did not allow images to be downloaded from their server.
I am working on my validation script, and if this occurs, i want to give some feedback to the user.
How can i detect this issue in PHP? e.g.
if (NO DOWNLOAD FROM SERVER ERROR) {
$return['imageDownloadError'];
}
Just a side question, whats the term called when you write a piece of code in plain english? and not in code?
Thanks! Craig
I would suggest doing something like the following:
try with "imagecreatefromjpeg"
if the returned variable === FALSE then check why with cURL using "curl_errno".
e.g.
$ch = curl_init($url);
curl_exec($ch);
if (curl_error($ch)){
echo curl_error($ch);
}
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.
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