Wednesday, August 8, 2012

Re: cakephp probleme with pagination and file_get_contents

It looks to me that the problem is that the $origins and $destinations
are not being escaped. For example:

M'hamid => M%27hamid
El Jadida => El%20Jadida

So you need to do something like this:

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

So this has nothing to do with pagination. The destinations which
require escaping happen to appear only on the next page.

Bettawfeeq

On Wed, Aug 8, 2012 at 12:01 PM, yassine yas <yanddam@gmail.com> wrote:
> salam
>
> my english is not good
>
> well, i develop a back-end for a mobile application with cakephp
>
> i have a fuction 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 received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.

No comments: