2016-10-23 20 views
0

我有一個用於我的網站的API,我想將它用於移動應用程序。但問題是這些API提供了大量不需要的數據,所以我想製作一個第三方API來過濾從第一個API獲取的數據。使用PHP和JSON創建第三方API

爲例,我想過濾從第一API獲得的文章列表數據, 我所做的就是以下幾點:

<?php 
    header('Content-type: application/json'); 
    if (isset($_GET['kat'])) { 
    $url = $_GET['kat']; 
    } else { 
    $url = null; 
    } 

    $app_list = file_get_contents('http://localhost/api/articles/lists'); 
    $app_list = json_decode($app_list, true); 
    foreach ($app_list as $app) { 
    $id = $app['id']; 
    $uid = $app['user_id']; 
    $cat = $app['category']; 
    $tgl = $app['created_date']; 
    $img = $app['image']; 
    $posttipe = $app['post_user_type']; 
    $komen = $app['count_comment']; 
    $likes = $app['count_likes']; 
    $c = substr($app['content'], 0, 100); 
    $content= $c . "..."; 
    if ($cat == $url || !$url) { 
     $list = array("id" => $id, "user_id" => $uid, "category" => $cat, "image" => $img, "post_user_type" => $posttipe, "count_comment" => $komen, "count_likes" => $likes, "created_date" => $tgl, "content" => $content); 
    } 
    } 
    echo json_encode($list); 
?> 

說,所以我的問題是,該API我做不首先顯示從API獲取的所有文章列表。

我使用$_GET['kat'];篩選文章類型

我應該在我的腳本編輯?

+0

它顯示了什麼? – Ivan

回答

0

你在一個循環中,每次輪循環加載$list所以

$list = array("id" => $id, .....); 

是在寫作$list

更改此行

$list[] = array("id" => $id, .....); 

現在您將創建一個數組的數組,包含您的循環數據的所有結果。

+0

感謝您解答 – wanttobe