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

我只需要关于这个问题的帮助。这是我想在我的 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);


}


慕姐8265434
浏览 118回答 2
2回答

白猪掌柜的

编辑:最终通过使用多卷曲解决了卷曲多个请求。这是事件中的教程链接 Per Id 将被处理并将调用一个 curl 请求到 API。并构造一个 assoc 数组。完成后,我将 jsonResponse 编码为 JSON。function multiCurl($eventArray){&nbsp; &nbsp; // array of curl handles&nbsp; &nbsp; $multiCurl = array();&nbsp; &nbsp; // data to be returned&nbsp; &nbsp; $result = array();&nbsp; &nbsp; // multi handle&nbsp; &nbsp; $mh = curl_multi_init();&nbsp; &nbsp; foreach ($eventArray as $event) {&nbsp; &nbsp; &nbsp; &nbsp; //$event are the ID per each event&nbsp; &nbsp; &nbsp; &nbsp; // URL from which data will be fetched&nbsp; &nbsp; &nbsp; &nbsp;// $curl = curl_init();&nbsp; &nbsp; &nbsp; &nbsp; $multiCurl[$event] = curl_init();&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt_array($multiCurl[$event], array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_URL =>'https://api.civicore.com/voc/api/v3/data/opportunities/'.$event.'?key='.$GLOBALS['API_KEY'].'&fields=["opportunityName","typeOfWork","firstDateInOpportunity","lastDateInOpportunity","startTime","endTime","physicalDifficulty","minimumAge","location","state","city","county","campingAvailable","groupsAllowed","registrationFormID","cRQ_payment","paymentAmount","additionalInformation","photo","displayToPublic","latitude","longitude","summary","description","photo"]&where={"whereType":"AND","clauses":[{"fieldName":"displayToPublic","operator":"checked"}]}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_RETURNTRANSFER => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_ENCODING => "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_MAXREDIRS => 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_TIMEOUT => 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_FOLLOWLOCATION => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_CUSTOMREQUEST => "GET"&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; curl_multi_add_handle($mh, $multiCurl[$event]);&nbsp; &nbsp; }&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; curl_multi_exec($mh,$index);&nbsp; &nbsp; } while($index > 0);&nbsp; &nbsp; &nbsp; // get content and remove handles&nbsp; &nbsp; &nbsp; foreach($multiCurl as $key=>$value) {&nbsp; &nbsp; &nbsp; &nbsp; $records = json_decode(curl_multi_getcontent($value));//response of each request&nbsp; &nbsp; &nbsp; &nbsp; $record&nbsp; = $records->records[0];&nbsp; &nbsp; &nbsp; &nbsp;if(strtolower($record->displayToPublic->displayValue) == 'yes'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $eve =&nbsp; [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "page_item_url"=> $record->opportunities_id->value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data"=>[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "startTime"=>$record->startTime->displayValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "endTime"=>$record->endTime->displayValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "summary"=> $record->summary->displayValue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($GLOBALS["jsonResponse"],$eve);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; curl_multi_remove_handle($mh, $value);&nbsp; &nbsp; &nbsp; }//foreach&nbsp; &nbsp; curl_multi_close($mh);}

翻翻过去那场雪

示例代码中确实存在一些语法错误。但是请尝试这个。<?phpfunction getEventByID($eventID){&nbsp; &nbsp; $curl = curl_init();&nbsp; &nbsp; curl_setopt_array($curl, array(&nbsp; &nbsp; &nbsp; &nbsp; 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"}]}',&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_RETURNTRANSFER => true,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_ENCODING => "",&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_MAXREDIRS => 10,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_TIMEOUT => 0,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_FOLLOWLOCATION => true,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_CUSTOMREQUEST => "GET",&nbsp; &nbsp; ));&nbsp; &nbsp; $response = json_decode(curl_exec($curl));&nbsp; &nbsp; curl_close($curl);&nbsp; &nbsp; $record = $response->records[0];&nbsp; &nbsp; return json_decode(json_encode([&nbsp; &nbsp; &nbsp; &nbsp; "page_item_url" => $record->opportunities_id->value,&nbsp; &nbsp; &nbsp; &nbsp; "data" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "startTime" => $record->startTime->displayValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "endTime" => $record->endTime->displayValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "summary" => $record->summary->displayValue,&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ], JSON_HEX_QUOT | JSON_HEX_TAG));}$events = [2111, 2112, 2113, 2114, 2115]; //etc$jsonResponse = [];for ($i = 0; $i < count($events); $i++) {&nbsp; &nbsp; $jsonResponse[] = getEventByID($events[$i]);}print_r($jsonResponse);?>
打开App,查看更多内容
随时随地看视频慕课网APP