Prem Singh Bist
Prem Singh Bist

Reputation: 1313

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

  1. allow_url_fopen : On it's ok
  2. New ApI key to the Linux server.
  3. In local windows system, all response is ok.

What might me the issue. Any help is really appreciated.

Upvotes: 2

Views: 6243

Answers (1)

Robbie
Robbie

Reputation: 17710

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: https://www.php.net/curl_exec

Upvotes: 1

Related Questions