Php HTTP请求失败!HTTP/1.0 403禁止
我在6个月前为一个站点创建了一个store finder,数据存储在数据库中。用户键入他们的邮政编码,当您单击“搜索”时,他们会列出离他们最近的商店 今天早上我测试live站点时,收到php错误消息:Php HTTP请求失败!HTTP/1.0 403禁止,php,google-maps-api-3,error-handling,key,Php,Google Maps Api 3,Error Handling,Key,我在6个月前为一个站点创建了一个store finder,数据存储在数据库中。用户键入他们的邮政编码,当您单击“搜索”时,他们会列出离他们最近的商店 今天早上我测试live站点时,收到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=----MY
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);
}
}
类存储扩展CI\U控制器{
函数_u构造(){
父项::_构造();
$this->load->model('store_model');
$this->load->library('form_validation');
}
公共职能指数(){
$data['page_title']=“商店查找器”;
$data['章节编号]=5;
$data['content']=“store”;
函数格式为邮政编码($postcode){
//--------------------------------------------------
//清理用户输入
$postcode=strtoupper($postcode);
$postcode=preg_replace(“/[^A-Z0-9]/”,“$postcode”);
$postcode=preg_replace('/([A-Z0-9]{3})$/','\1',$postcode);
$postcode=trim($postcode);
//--------------------------------------------------
//检查提交的值是否有效
//英国邮政编码:安那|安那|安那|
//安娜娜|安娜娜|安娜娜娜
if(preg_match('/^[a-z](\d[a-z\d]?|[a-z]\d[a-z\d]?)\d[a-z]{2}$/i',$postcode)){
返回$postcode;
}否则{
返回NULL;
}
}
$this->form_validation->set_规则('postcode'、'postcode'、'required | trim | htmlspecialchars | xsclean');
$this->form_validation->set_error_分隔符(“&bull;”,”);
如果($this->form\u validation->run()==FALSE){
$data['error']=1;
如果($this->input->post('submit')){
$data['error_msg']=“请输入邮政编码。”;
}
}否则{
$data['error']=0;
如果($this->input->post('postcode')){
$postCodeClean=formatBritishPostcode($this->input->post('postcode');
如果($postCodeClean==NULL){
$data['error_msg']=“请提供有效的邮政编码。”;
}否则{
$url=”http://maps.google.co.uk/maps/geo?q=“.urlencode($this->input->post('postcode'))。”&output=json&key=--MYKEY----”;
$json=文件获取内容($url);
$store\u data=json\u decode(str\u replace(“”,“\”,htmlentities($json));
$lng=$store_data->Placemark[0]->点->坐标[0];
$lat=$store_data->Placemark[0]->点->坐标[1];
$data['stores']=$this->store\u model->get\u stores($lat,$lng);
}
}
}
$this->load->view('template',$data);
}
}
这听起来像是服务的问题,而不是脚本的问题。请尝试联系谷歌或搜索他们的支持系统,以确保您使用的是有效的URL。这是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(""","\"",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);
这不是代码的问题 这可能是因为它会阻止PHP脚本以防止刮取,或者如果您发出了太多请求,它会阻止您的IP
您应该与服务器团队联系,他们可以帮助您解锁IP。您可以在浏览器中访问该URL吗?当我输入URL时,我得到以下信息:我们很抱歉。。。但您的计算机或网络可能正在发送自动查询。为了保护我们的用户,我们现在无法处理您的请求:it’真奇怪,它怎么能工作几个月,然后停下来!可能的副本,请参阅