0

I am using MAMP as my local host and PHP 7.3.8 as my localhost.

On my site I am trying to connect to google maps as follows:

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false&key=MY-API-KEY"
$data = @file_get_content($url)

However for some reason the connection is not established when I try this on a live site (using GoDaddy as a server) all is well and I get the wanted result.

I have tried to switch on allow_url_fopenin the php.ini and have checked the firewall on my localhost and made sure the ports are open, however no joy.

Anyhelp to solve this problem welcome

Ria
  • 516
  • 6
  • 24
  • It might be because it is not hosted on the domain. – Example person Jan 10 '20 at 10:05
  • @Chi.C.J.RajeevaLochana Thank you for your reply, however the localhost is on my computer this is where I would like to test it however for some reason it will not return the $data contents – Ria Jan 10 '20 at 10:10
  • Maybe the google api doesn't allow you to use it in the host: localhost. Maybe turn on display_errors by adding ```ini_set("display_errors", "1");``` to the top of your document. – Example person Jan 10 '20 at 10:11
  • @Chi.C.J.RajeevaLochana thank you for your reply however I have already done that and it just won't load the page not he localhost no errors are reported – Ria Jan 10 '20 at 10:25
  • Now, I think it is a problem with PHP. – Example person Jan 10 '20 at 10:36
  • @Chi.C.J.RajeevaLochana thank you again, would you know what the problem with php might be this is way I changed the allow_url_fopen. – Ria Jan 10 '20 at 11:09
  • Does a basic PHP script work? And also try creating a info.php file and just typing in: ``` – Example person Jan 10 '20 at 11:12
  • @Chi.C.J.RajeevaLochana all the normal this work I have tried to replace the link with http://bbc.co.uk and that seems to work fine, it seems like it doesn't like the parameters behind it like the &orgins etc any idea what might be that? – Ria Jan 10 '20 at 11:23
  • I think google is the one which is not allowing you to use the api – Example person Jan 10 '20 at 11:24
  • Did you replace MY-API-KEY to your api – Example person Jan 10 '20 at 11:25
  • 1
    My API is replaced with the one google gave me, I just didn't want to share that one online so anyone could use it – Ria Jan 10 '20 at 11:29
  • Thats good, also I am going to move this to the chat – Example person Jan 10 '20 at 11:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205747/discussion-between-chi-c-j-rajeeva-lochana-and-ria). – Example person Jan 10 '20 at 11:30

0 Answers0