我只需要有关此问题的帮助。这是我想要在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返回的代码用于单个对象时,getEventByID函数的返回值应为json格式,类似于上面的json示例中的示例,请参见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构造和返回单个事件。这是正确的,我得到了https:// prnt.sc/ris9g7如果我删除json_encode中的方括号并仅传递键值的assoc数组然后将其推到数组中,该怎么办
编辑:最后通过使用多重卷曲解决了卷曲多个请求。这是事件中的Per Id教程链接,将对其进行处理并将向API调用curl请求。并构造一个assoc数组。完成后,我已将jsonResponse编码为JSON。
function multiCurl($eventArray){
// array of curl handles
$multiCurl = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
foreach ($eventArray as $event) {
//$event are the ID per each event
// URL from which data will be fetched
// $curl = curl_init();
$multiCurl[$event] = curl_init();
curl_setopt_array($multiCurl[$event], array(
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"}]}',
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"
));
curl_multi_add_handle($mh, $multiCurl[$event]);
}
do {
curl_multi_exec($mh,$index);
} while($index > 0);
// get content and remove handles
foreach($multiCurl as $key=>$value) {
$records = json_decode(curl_multi_getcontent($value));//response of each request
$record = $records->records[0];
if(strtolower($record->displayToPublic->displayValue) == 'yes'){
$eve = [
"page_item_url"=> $record->opportunities_id->value,
"data"=>[
"startTime"=>$record->startTime->displayValue,
"endTime"=>$record->endTime->displayValue,
"summary"=> $record->summary->displayValue
]
];
array_push($GLOBALS["jsonResponse"],$eve);
}
curl_multi_remove_handle($mh, $value);
}//foreach
curl_multi_close($mh);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句