php - HTTP 请求失败! HTTP/1.0 403 禁止

标签 php google-maps-api-3 error-handling key

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

当我在暂存站点和本地主机上尝试此操作时,我得到了相同的响应!现在我已经有 6 个月没有碰过代码了,所以我认为我的 key 已经过期或损坏了。我创建了一个新的 API key 并将其添加到我的标题和 Controller 中,它说错误是 - 不走运。

我哪里错了?!某些东西正在停止请求并且不确定问题出在哪里?

这是我的 Controller 和代码:
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);
    }
} 

最佳答案

这是 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);

关于php - HTTP 请求失败! HTTP/1.0 403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19562200/

相关文章:

windows - 为什么这个try/catch block 神奇地吞噬了异常?

php - 使用 PHP 制作的基于浏览器的策略游戏如何运行?

php - MySQL加入: how to simplyfy the given query?

php - 文件夹中的图像已删除,但图像路径未在 PHP 中更新为 null

javascript - Google 认为 api 无法正常工作

python - 服务器启动并运行时通过 pysm 访问未知 SMB 服务器时出现问题

php - WordPress 批量产品上传 (woocommerce) - 650K

javascript变量在函数内丢失

html - 在 Flex 中嵌入 Google Maps API Javascript

asp.net-mvc-2 - 如何在Asp.Net MVC 2中显示一般错误页面