I just need help regarding this problem. Here's what I wanted to achieve in my php output as this is the structure needed. An array of objects of events returned as 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>"
}
}
]
The JSON returned should be like that, it was a call from an API using cUrl. I have a function which first gets all the ID's and I pass it into a variable;
function getOpportunityYearly(){
//curl here...
//I pushed all the ID in my array to the global scope
}
So now, I have my array of ID's:
$events = [2111,2112,2113,2114,2115];//etc
$jsonResponse = [];
In which I would like to loop through and call another cUrl from the API which then should be pushed through the $jsonResponse my problem is when I pass the returned response from the getEventByID and convert it to json the structure is not correct.
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);
}
The return from getEventByID function should be a json formatted like from the one above json sample when I try to use the code returned from getEventByID for a single object it is fine see https://prnt.sc/ris9g7 but when I push first the response to a global array variable then encode it it got something like this https://prnt.sc/risjw5
I tried creating an array and push all the returned from getEventByID but the output is not a JSON string it was an array. Am I doing something wrong.
When I use the forloop the page was slow, and it was getting maximum timeout of 30secs I have tried using $curl, CURLOPT_TIMEOUT_MS and set it to 5mins. But is there any other way to make this cURL not slow? Or is this normal as we are getting all the ID events objects from the API? Can you advise anything to improve this?
This is the returned type from getEventByID https://prnt.sc/ris8zx This is how I construct and return a single event from curl response https://prnt.sc/ris9c0 This is the correct which is I got https://prnt.sc/ris9g7 how ever if I remove the brackets in the json_encode and just pass an assoc array of key value then push it to an array