【问题标题】:PHP CURL How to filter json results?PHP CURL 如何过滤 json 结果?
【发布时间】:2019-03-21 01:10:45
【问题描述】:

需要过滤我的 curl json 请求。 我的问题是.. 我的输出列表总是完整的 JSON 列表(链接 1-3)。 我只需要请求链接 1 和链接 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


    【解决方案1】:

    最好在从 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);
    ?>
    

    演示: https://3v4l.org/BNotG

    【讨论】:

      【解决方案2】:

      看起来您正在尝试在 api 中执行此操作:/ 它甚至提供该功能吗?如果是这样,请询问他们,rtm 或给我们网站的真实链接,以便我们查看文档..

      如果不是我怀疑的,那么只需在结果上使用array filter

      【讨论】:

      • 感谢您的回答.. 是的,这可能是此 api 上不存在此功能。我将检查 php 数组过滤器
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2012-08-23
      • 2013-02-02
      • 2020-08-13
      相关资源
      最近更新 更多