I develop a back-end for a mobile application with cakephp
I have a function that list cities and I want to calculate distance from the user the this cities the code is:
$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");
the problem is when i test with the first page (pagination) the result is good but when i access to others page >=2 i got this warning and i cant get the distance
the error is :
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, line 81]
Code Context
$origins = “origins=casablanca,maroc”
$destinations = “&destinations=Zagora,Morocco|Meknes,Morocco|Chefchaouen,Morocco|Tinghir,Morocco|M’hamid,Morocco|El
Jadida,Morocco|Erfoud,Morocco|Skoura,Morocco|Tetouan,Morocco|Imlil,Morocco”
$url = “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”
$hotels = array( array( “Hotel” => array(), array() ), array(
“Hotel” => array(), array() ), array( “Hotel” => array(), array()
), array( “Hotel” => array(), array() ), array( “Hotel” =>
array(), array() ), array( “Hotel” => array(), array() ), array(
“Hotel” => array(), array() ), array( “Hotel” => array(), array()
), array( “Hotel” => array(), array() ), array( “Hotel” =>
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”,
“Date: Wed, 08 Aug 2012 15:42:43 GMT”, “Server: GFE/2.0” )file_get_contents – [internal], line ?? ApiController::yassine() –
APP\Controller\ApiController.php, line 81
ReflectionMethod::invokeArgs() – [internal], line ??
Controller::invokeAction() – CORE\Cake\Controller\Controller.php, line
473 Dispatcher::_invoke() – CORE\Cake\Routing\Dispatcher.php, line 104
Dispatcher::dispatch() – CORE\Cake\Routing\Dispatcher.php, line 86
[main] – APP\webroot\index.php, line 96
You must correct this line.