HTTP请求失败! HTTP / 1.0 403禁止 [英] HTTP request failed! HTTP/1.0 403 Forbidden
问题描述
我在6个月前为某个网站创建了一个商店查找器,数据存储在数据库中。用户输入他们的邮政编码,当您点击搜索时,他们会列出最近的商店。
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.
当我今天早上测试了现场网站时,我收到了php错误messag: p>
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
我在分段站点和我的本地主机上尝试过这个我得到相同的回应!现在我没有碰到6个月的代码,所以我假设我的密钥已经过期或损坏。我创建了一个新的API密钥,并将其添加到我的头文件和控制器中,其中显示错误为-no luck。
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">• ','</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);
}
}
推荐答案
这是API V2和3的一个问题。以上代码已更改为此,并且像一个魅力:
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(""","\"",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);
这篇关于HTTP请求失败! HTTP / 1.0 403禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!