0

我用 cakephp 为移动应用程序开发后端

我有一个列出城市的函数,我想计算与这个城市的用户的距离,代码是:

$origins='origins=casablanca,maroc';
    $destinations='&destinations=';
    $url='http://maps.googleapis.com/maps/api/distancematrix/json?';

    $this->paginate = array(
            'limit' => 10,
            'fields'=>array('cityId','cityFileName','cityName','stateId','stateFileName','stateName',
                'countryCode','countryFileName','countryName','count(id) as num_hotels'),
            'order' => 'num_hotels desc',
            'group' =>array('cityId')

    );
    $hotels = $this->paginate();

    for ($i = 0; $i < count($hotels); ++$i) {

        $destination=($i==0?'':'|');

        $destination.=$hotels[$i]['Hotel']['cityName'].','.$hotels[$i]['Hotel']['countryName'];

        $destinations.=$destination;
    }

    $url.=$origins.$destinations.'&mode=driving&language=fr-FR&sensor=false';

    $json = file_get_contents($url,0,null,null);
    $details = json_decode($json, TRUE);

    for ($i = 0; $i < count($hotels); ++$i) {
        $hotels = Set::insert($hotels, $i.'.Hotel.distance',$details['rows']['0']['elements']['0']['distance']['value']);
    }

    $this->set(compact('hotels'));
    $this -> viewPath = "hotels";
    $this -> render("json/index","json/default");

问题是当我用第一页(分页)进行测试时,结果很好,但是当我访问其他页面> = 2时,我收到了这个警告,我无法获得距离

错误是:

Warning (2): file_get_contents(http://maps.googleapis.com/maps/api/distancematrix/json?origins=casablanca,maroc&destinations=Zagora,Morocco|Meknes,Morocco|Chefchaouen,Morocco|Tinghir,Morocco|M'hamid,Morocco|El Jadida,Morocco|Erfoud,Morocco|Skoura,Morocco|Tetouan,Morocco|Imlil,Morocco&mode=driving&language=fr-FR&sensor=false) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

[APP\Controller\ApiController.php,第 81 行]

代码上下文

$origins = "origins=casablanca,maroc" $destinations = "&destinations=Zagora,Morocco|Meknes,Morocco|Chefchaouen,Morocco|Tinghir,Morocco|M'hamid,Morocco|El Jadida,Morocco|Erfoud,Morocco|Skoura,Morocco |得土安,摩洛哥|伊姆利尔,摩洛哥" $url = " http://maps.googleapis.com/maps/api/distancematrix/json?origins=casablanca,maroc&destinations=Zagora,Morocco|Meknes,Morocco|Chefchaouen,Morocco|Tinghir ,摩洛哥|M'hamid,Morocco|El Jadida,Morocco|Erfoud,Morocco|Skoura,Morocco|Tetouan,Morocco|Imlil,Morocco&mode=driving&language=fr-FR&sensor=false" $hotels = array(array("Hotel" => array(),数组()),数组(“酒店”=>数组(),数组()),数组(“酒店”=>数组(),数组()),数组(“酒店”=>数组(),数组( ) ),数组(“酒店”=> 数组(),数组()),数组(“酒店”=> 数组(),数组()),数组(“酒店”=> 数组(),数组()) ,数组(“酒店”=>数组(),数组()),数组(“酒店”=>数组(),数组()),数组(“酒店”=>数组(),数组()))$ i = 10 $destination = "|伊姆利尔,摩洛哥"$http_response_header = array("HTTP/1.0 400 Bad Request", "Content-Type: text/html; charset=UTF-8", "Content-Length: 925", "Date: Wed, 08 Aug 2012 15:42:格林威治标准时间 43","服务器:GFE/2.0")

file_get_contents - [内部],行 ?? ApiController::yassine() - APP\Controller\ApiController.php,第 81 行 ReflectionMethod::invokeArgs() - [内部],行 ?? Controller::invokeAction() - CORE\Cake\Controller\Controller.php,第 473 行 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php,第 104 行 Dispatcher::dispatch() - CORE\Cake\Routing \Dispatcher.php,第 86 行 [主] - APP\webroot\index.php,第 96 行

4

1 回答 1

0

您必须更正这条线。

$url.="origins=" .  urlencode($origins). "&destinations=" .
      urlencode($destinations)."&mode=driving&language=fr-FR&sensor=false";
于 2012-08-27T09:03:35.453 回答