2

I created a store finder 6 months ago for a site and the data is stored in the database. The user types in their postcode and when you click search they list the nearest stores to them.

When i tested the live site this morning i get php error messag:

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://maps.google.co.uk/maps/geo?q=sr3+4as&output=json&key=----MYKEY---): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Filename: controllers/store.php

When i tried this at the staging site and my localhost i get the same response! Now i havent touched the code for 6 months so i assumed that my key had expired or corrupted. I created a new API key and added this in my header and in the controller where it says the error is -no luck.

Where am i going wrong?! Something is stopping the request and unsure where the issue is?

Here is my controller with the code:

class Store extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('store_model');
        $this->load->library('form_validation');
    }

    public function index(){
        $data['page_title'] = "Store Finder";
        $data['section_no'] = 5;
        $data['content'] = "store";

         function formatBritishPostcode($postcode) {    
            //--------------------------------------------------
            // Clean up the user input

            $postcode = strtoupper($postcode);
            $postcode = preg_replace('/[^A-Z0-9]/', '', $postcode);
            $postcode = preg_replace('/([A-Z0-9]{3})$/', ' \1', $postcode);
            $postcode = trim($postcode);

            //--------------------------------------------------
            // Check that the submitted value is a valid
            // British postcode: AN NAA | ANN NAA | AAN NAA |
            // AANN NAA | ANA NAA | AANA NAA

            if (preg_match('/^[a-z](\d[a-z\d]?|[a-z]\d[a-z\d]?) \d[a-z]{2}$/i', $postcode)) {
                return $postcode;
            } else {
                return NULL;
            }       
        } 

        $this->form_validation->set_rules('postcode','Postcode','required|trim|htmlspecialchars|xssclean'); 
        $this->form_validation->set_error_delimiters('<div id="errors">&bull;&nbsp;','</div>');

        if($this->form_validation->run() == FALSE) {
            $data['error'] = 1;
            if($this->input->post('submit')) {
                $data['error_msg'] = "Please enter a Post Code.";   
            }
        } else {
            $data['error'] = 0;
            if($this->input->post('postcode')) {
                $postCodeClean = formatBritishPostcode($this->input->post('postcode'));
                if ($postCodeClean === NULL) {
                    $data['error_msg'] = "Please supply a valid Post Code.";
                } else {

                $url = "http://maps.google.co.uk/maps/geo?q=".urlencode($this->input->post('postcode'))."&output=json&key=---MYKEY---";

                    $json = file_get_contents($url);
                    $store_data = json_decode(str_replace("&quot;","\"",htmlentities($json)));

                    $lng = $store_data->Placemark[0]->Point->coordinates[0];            
                    $lat = $store_data->Placemark[0]->Point->coordinates[1];

                    $data['stores'] = $this->store_model->get_stores($lat, $lng);
                }
            }
        }

        $this->load->view('template', $data);
    }
} 
user2212564
  • 261
  • 1
  • 7
  • 29
  • Can you access that URL in a browser? – TecBrat Oct 24 '13 at 09:58
  • When i enter the url i get this: We're sorry... ... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now. :S strange how it can work for months then stop! – user2212564 Oct 24 '13 at 10:02
  • possible duplicate of [Recieving a 403 forbidden error when using latitude and longitude: geocoding](http://stackoverflow.com/questions/18743128/recieving-a-403-forbidden-error-when-using-latitude-and-longitude-geocoding), see https://developers.google.com/maps/articles/geocodingupgrade – geocodezip Oct 24 '13 at 10:33

3 Answers3

1

This was an issue with API V2 & 3. The code above was changed to this and worked like a charm:

$url ="https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($this->input->post('postcode'))."&sensor=false";


$json = file_get_contents($url);
$store_data = json_decode(str_replace("&quot;","\"",htmlentities($json)));

$lat = $store_data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$lng = $store_data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

$data['stores'] = $this->store_model->get_stores($lat, $lng);
user2212564
  • 261
  • 1
  • 7
  • 29
0

That sounds like a problem with the service, and not with your script. Try contacting Google or searching their support system to make sure you are using a valid URL.

TecBrat
  • 3,643
  • 3
  • 28
  • 45
-1

This is not a problem with your Code.

It could be that it blocks PHP scripts to prevent scraping, or your IP if you have made too many requests.

You should talk to the server team they can help you with to unblock the IP.