我正在使用CakePHP进行培训。目前我有一个问题。 我不知道如何使用json数据进行分页。 我的应用程序将调用API并接收JSON数据。 格式看起来像
{"success":1,"films":[{"film_id":"384","title":"GROSSE WONDERFUL","description":"A Epic Drama of a Cat And a Explorer who must Redeem a Moose in Australia","release_year":"2006","language_id":"1","original_language_id":null,"rental_duration":"5","rental_rate":"4.99","length":"49","replacement_cost":"19.99","rating":"R","special_features":"Behind the Scenes","last_update":"2006-02-15 05:03:42"},{"film_id":"984","title":"WONDERFUL DROP","description":"A Boring Panorama of a Woman And a Madman who must Overcome a Butler in A U-Boat","release_year":"2006","language_id":"1","original_language_id":null,"rental_duration":"3","rental_rate":"2.99","length":"126","replacement_cost":"20.99","rating":"NC-17","special_features":"Commentaries","last_update":"2006-02-15 05:03:42"}]}
这是php代码
$data = array('filmId' => $search_data['filmId'], 'filmTitle' => $search_data['filmTitle'], 'releaseYear' => $search_data['releaseYear'], 'rating' => $search_data['rating']);
$HttpSocket = new HttpSocket();
$records = $HttpSocket->post($apiFilmsByConditions, $data);
$records = json_decode($records, true);
我尝试使用CakePHP的分页,但似乎这将从数据库获取数据,而不是来自API。
这是API链接http://oxuhandmade.com/api/get_all_films.php
你能帮我解决这个案子吗?
答案 0 :(得分:0)
API不查询数据库吗? API是一个“应用程序编程接口”,这意味着它允许您基于由某些请求触发的简单接口从“外部”操作数据库。因此,没有必要通过API限制结果,但不是通过数据库。
要限制API返回的结果,您需要将参数与请求一起传递,在后端处理它,使用所需的限制查询数据库,并将结果返回给发出请求的人。或者只是硬编码API上的所需限制,如果它不需要是动态的。
来自CakePHP的Paginator
使这项工作变得更加容易,你只需要为它提供一些条件和参数来分页结果,它就会为你带来魔力。