我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">• ','</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(""","\"",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);
}
}
你能在瀏覽器中訪問該URL嗎? – TecBrat
當我輸入url我得到這個:很抱歉... ...但您的計算機或網絡可能會發送自動查詢。爲了保護我們的用戶,我們目前無法處理您的請求。 :奇怪它怎麼能工作幾個月然後停下來! – user2212564
[使用經緯度時收到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