如何在php的cURL响应中创建对象的json

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

我只需要有关此问题的帮助。这是我想要在php输出中实现的功能,因为这是所需的结构。以JSON形式返回的事件对象数组。

[
    {
        "page_item_url":"2111",
        "data":{
            "startTime":"11:00"
            "endTime":"12:00",
            "summary":"<p>This has html tags in it from API<p>"
        }
    },{
        "page_item_url":"2112",
        "data":{
            "startTime":"11:00"
            "endTime":"12:00",
            "summary":"<p>This has html tags in it from API<p>"
        }
    }
]

返回的JSON应该是这样,它是使用cUrl从API进行的调用。我有一个首先获取所有ID的函数,然后将其传递给变量;

function getOpportunityYearly(){
    //curl here...
    //I pushed all the ID in my array to the global scope
}

所以现在,我有了ID数组:

   $events = [2111,2112,2113,2114,2115];//etc
   $jsonResponse = [];

[我想循环访问并从API调用另一个cUrl,然后应该通过$ jsonResponse推送我的问题是,当我传递getEventByID返回的响应并将其转换为json时,结构不正确。] >

for($i=0;$i<count($events);$i++){
    getEventByID($events[$i]);
}

function getEventByID($eventID){
    curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL =>'https://sampleapi.com/api/v3/data/opportunities/'.$eventID.'?key='.$GLOBALS['API_KEY'].'&fields=["startTime","endTime","physicalDifficulty","summary"]&where={"whereType":"AND","clauses":[{"fieldName":"displayToPublic","operator":"checked"}]}',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",

    $response = json_decode(curl_exec($curl));
    curl_close($curl);
    $record = $response->records[0];

    return json_encode([[
      "page_item_url"=> $record->opportunities_id->value,
        "data"=>[
          "startTime"=>$record->startTime->displayValue,
          "endTime"=>$record->endTime->displayValue,
          "summary"=>$record->summary->displayValue,
          ]
    ]],JSON_HEX_QUOT | JSON_HEX_TAG);

}

getEventByID函数的返回值应为json格式,如上面的json示例所示,当我尝试将getEventByID返回的代码用于单个对象时,请参见https://prnt.sc/ris9g7,但当我先将响应推送到全局对象时,数组变量,然后对其进行编码,得到的内容如下:https://prnt.sc/risjw5

我尝试创建一个数组,并将所有从getEventByID返回的结果推入,但输出不是JSON字符串,而是一个数组。我在做错什么吗?

当我使用forloop时,页面运行缓慢,并且我尝试使用的最大超时时间为30秒$ curl,CURLOPT_TIMEOUT_MS并将其设置为5分钟。但是还有其他方法可以使此cURL不慢吗?还是从API获取所有ID事件对象时,这是正常现象吗?您可以提出建议以改善此情况吗?

这是getEventByID https://prnt.sc/ris8zx返回的类型这就是我从curl响应https://prnt.sc/ris9c0构造并返回单个事件的方式这是正确的,如果我删除json_encode中的括号并仅传递键值的assoc数组然后将其推到数组,我会得到https://prnt.sc/ris9g7

我只需要有关此问题的帮助。这是我想要在php输出中实现的功能,因为这是所需的结构。以JSON返回的事件对象数组。 [{“ ...

php json curl
1个回答
0
投票

您在示例代码中确实有一些语法错误。但是,请尝试此。

<?php
function getEventByID($eventID)
{
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://sampleapi.com/api/v3/data/opportunities/' . $eventID . '?key=' . $GLOBALS['API_KEY'] . '&fields=["startTime","endTime","physicalDifficulty","summary"]&where={"whereType":"AND","clauses":[{"fieldName":"displayToPublic","operator":"checked"}]}',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
    ));
    $response = json_decode(curl_exec($curl));
    curl_close($curl);
    $record = $response->records[0];

    return json_decode(json_encode([
        "page_item_url" => $record->opportunities_id->value,
        "data" => [
            "startTime" => $record->startTime->displayValue,
            "endTime" => $record->endTime->displayValue,
            "summary" => $record->summary->displayValue,
        ]
    ], JSON_HEX_QUOT | JSON_HEX_TAG));
}

$events = [2111, 2112, 2113, 2114, 2115]; //etc
$jsonResponse = [];

for ($i = 0; $i < count($events); $i++) {
    $jsonResponse[] = getEventByID($events[$i]);
}

print_r($jsonResponse);

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