我在6个月前为网站创建了一个商店查找器,数据存储在数据库中。用户输入他们的邮政编码,当您点击搜索时,他们会列出最近的商店。
当我今天早上测试现场网站时,我得到了php错误消息:
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
当我在临时站点和我的localhost上尝试此操作时,我得到相同的响应!现在我没有触及代码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);
}
}
答案 0 :(得分:1)
这是API V2&amp; S的问题。 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(""","\"",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 :(得分:0)
这听起来像是服务的问题,而不是你的脚本。尝试与Google联系或搜索其支持系统,以确保您使用的是有效网址。
答案 2 :(得分:-1)
这不是您的代码的问题。
可能会阻止PHP脚本以防止抓取,或者如果您提出的请求太多,则会阻止您的IP。
您应该与服务器团队联系,他们可以帮助您取消阻止IP。