PHP CURL如何过滤json结果?

问题描述 投票:1回答:2

需要过滤我的curl json请求。我的问题是..我的输出列表始终是完整的JSON列表(链接1-3)。我只需要请求Link 1和Link 3。

请检查我的过滤器示例

$responseData = apiRequest("link/list", array('id' => json_encode(array('1001', '1003'))));

此过滤器对我不起作用。我怎样才能解决我的问题?我很高兴每一个提示

非常感谢

Api请求:

function apiRequest($command, $requestData = array()) {
            $apiKey = "";
                $headers = array(
     'Authorization: APIKEY '.$apiKey
);
            if (!is_array($requestData)) {
                $requestData=array();
            }
            $requestData['apiKey'] = $apiKey;
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_HTTPHEADER => $headers,
                CURLOPT_URL => 'https://api.example.com/'.$command,        
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => $requestData)
                );

            if (($responseData = curl_exec($curl))===false) {
                curl_close($curl);
                /* echo "cURL error: ".curl_error($curl); */
                return null;
            }

            return json_decode($responseData, true);
        }

        $responseData = apiRequest("link/list", array('id' => json_encode(array('1001', '1003'))));

Json列表:

 {
    "count": 3,
    "links": [
        {
            "id": 1001,
            "name": "Link 1",
            "title": "Link Title",
            "head": "Links",
            "pic": "https://image.com/pic.jpg",
            "views": "10,000+",
            "country": "US"
        }

          {
            "id": 1002,
            "name": "Link 2",
            "title": "Link Title 2",
            "head": "Links",
            "pic": "https://image.com/pic.jpg",
            "views": "10,000+",
            "country": "US"
        }

       {
            "id": 1003,
            "name": "Link 3",
            "title": "Link Title 3",
            "head": "Links",
            "pic": "https://image.com/pic.jpg",
            "views": "10,000+",
            "country": "US"
        }
    ]
}
php json curl php-curl
2个回答
0
投票

最好在从API端获取结果时过滤掉结果但如果无法在API上过滤掉结果,那么请使用php array_filter()尝试这种方式,

<?php
$api_response = '{"count":3,"links":[{"id":1001,"name":"Link 1","title":"Link Title","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"},{"id":1002,"name":"Link 2","title":"Link Title 2","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"},{"id":1003,"name":"Link 3","title":"Link Title 3","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"}]}';

$filter = [1001,1003];
$links = json_decode($api_response)->links;
$filtered = array_filter($links, function ($item) use ($filter) {
    return in_array($item->id, $filter);
});

print_r($filtered);
?>

但是:Kua zxsw指出


0
投票

看起来你试图在api中执行它:/它甚至提供了这个功能吗?如果是这样问他们,rtm或给我们网站的真实链接,以便我们检查文档..

虽然如果不是我怀疑,那么只需在结果上使用https://3v4l.org/BNotG

© www.soinside.com 2019 - 2024. All rights reserved.