简体   繁体   中英

php file_get_contents not working on real server! does work on localhost?

hey guys, i developed a website on my local apache setup on my mac. I'm using two requests to foreign domains. One goes out to geoplugin.net to get the current geolocation. This works just fine on my local setup. However when I transfer the files to my real server the website prints the following:

Warning: file_get_contents(http://www.geoplugin.net/php.gp?ip=185.43.32.341) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/.sites/74/site484/web/testsite/wp-content/themes/test/header.php on line 241

what can I do here? What am I doing wrong?

Furthermore I'm using a curl request on my website which doesn't retrieve data as well. Both works fine on my local mamp setup.

any ideas?

You need to create an account at geoplugin.com and subscribe your domain to use the webservice without limitation, then you will stop to receive de 403 forbidden error. Don't worry about costs, it's a free service, i'm using it in three sites.

The server responds with an "403 FORBIDDEN" status code. So file_get_contents() works fine, but the server you are trying to access (or a proxy or something in between) dont allow it.

This can have many reasons. For example (like the comment of the question) you are banned, or blocked (because of to much requests), or something.

HTTP/1.0 403 Forbidden 

means you are not allowed to access this files! Try to add an user agent header.

That is because geoPlugin is limited to 120 lookups per minute. http://www.geoplugin.com/premium

So, any web-site feature based on this solution can be damaged suddenly.

I would recommend to use both www.geoplugin.net/json.gp?ip={ip} and freegeoip.net/json/{ip} . And check if first one returns null (means that limit already reached) then use another one.

try to urlencode the query string.

also I would recommend using curl extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM