我使用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");
问题是当我用第一页测试(分页)时结果很好但是当我访问其他页面&gt; = 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 =“originins = casablanca,maroc” $ destinations =“&amp; destinations =摩洛哥扎戈拉|摩洛哥梅克内斯|摩洛哥舍夫沙万| Tinghir,摩洛哥|摩洛哥M'hamid | El 杰迪代,摩洛哥|厄夫特,摩洛哥|斯库拉,摩洛哥|得土安,摩洛哥| Imlil的,摩洛哥” $ url =“http://maps.googleapis.com/maps/api/distancematrix/json?origins=casablanca,maroc&destinations=Zagora,Morocco|Meknes,Morocco|Chefchaouen,Morocco|Tinghir,Morocco|M'hamid,摩洛哥| El 杰迪代,摩洛哥|厄夫特,摩洛哥|斯库拉,摩洛哥|安,摩洛哥| Imlil的,摩洛哥及安培;模式=驱动&安培;语言= FR-FR&安培;传感器=假” $ hotels = array(array(“Hotel”=&gt; array(),array()),array( “酒店”=&gt; array(),array()),array(“Hotel”=&gt; array(),array() ),array(“Hotel”=&gt; array(),array()),array(“Hotel”=&gt; array(),array()),array(“Hotel”=&gt; array(),array()),array( “酒店”=&gt; array(),array()),array(“Hotel”=&gt; array(),array() ),array(“Hotel”=&gt; array(),array()),array(“Hotel”=&gt; array(),array()))$ i = 10 $ destination =“| Imlil,Morocco” $ http_response_header = array(“HTTP / 1.0 400 Bad Request”, “Content-Type:text / html; charset = UTF-8”,“Content-Length:925”, “日期:星期三,2012年8月8日15:42:43 GMT”,“服务器:GFE / 2.0”)
file_get_contents - [内部],行? ApiController :: yassine() - APP \ Controller \ ApiController.php,第81行 ReflectionMethod :: invokeArgs() - [internal],line ?? Controller :: invokeAction() - CORE \ Cake \ Controller \ Controller.php,line 473 Dispatcher :: _ invoke() - CORE \ Cake \ Routing \ Dispatcher.php,第104行 Dispatcher :: dispatch() - CORE \ Cake \ Routing \ Dispatcher.php,第86行 [主要] - APP \ webroot \ index.php,第96行
答案 0 :(得分:0)
您必须更正此行。
$url.="origins=" . urlencode($origins). "&destinations=" .
urlencode($destinations)."&mode=driving&language=fr-FR&sensor=false";