2013-10-24 176 views
1

我6個月前爲一個網站創建了商店查找器,數據存儲在數據庫中。用戶鍵入他們的郵政編碼,當您點擊搜索時,他們列出最近的商店。HTTP請求失敗! HTTP/1.0 403 Forbidden

當我今天早上,我讓PHP錯誤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 

當我想這在臨時站點和我的本地我得到同樣的迴應!現在我還沒有觸及6個月的代碼,所以我認爲我的密鑰已過期或損壞。我創建了一個新的API密鑰,並將其添加到我的頭文件和控制器中,它說錯誤是 - 沒有運氣。

我哪裏錯了?有什麼東西正在停止請求並且不確定問題出在哪裏?

這裏是我的控制器代碼:

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); 
    } 
} 
+0

你能在瀏覽器中訪問該URL嗎? – TecBrat

+0

當我輸入url我得到這個:很抱歉... ...但您的計算機或網絡可能會發送自動查詢。爲了保護我們的用戶,我們目前無法處理您的請求。 :奇怪它怎麼能工作幾個月然後停下來! – user2212564

+0

[使用經緯度時收到403禁止錯誤:地理編碼]的可能重複(http://stackoverflow.com/questions/18743128/recieving-a-403-forbidden-error-when-using-latitude-and-longitude -geocoding),請參閱https://developers.google.com/maps/articles/geocodingupgrade – geocodezip

回答

0

這聽起來似乎與該服務的問題,而不是與你的腳本。嘗試聯繫Google或搜索他們的支持系統,以確保您使用的是有效的網址。

1

這與API V2 & 3個問題上面的代碼更改爲這一點,工作就像一個魅力:

$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); 
-1

這不是您的代碼有問題。

這可能是因爲它阻止了PHP腳本來防止刮擦,或者如果你提出了太多的請求,你的IP。

您應該與他們可以幫助您解決IP問題的服務器團隊通話。