gpt4 book ai didi

php - 从本地主机到实时站点的 Curl 响应不一致

转载 作者:行者123 更新时间:2023-12-01 23:51:33 26 4
gpt4 key购买 nike

大家好,我有一个 php 文件,它返回一个 JSON,这是我根据curl 的响应构建的。我的问题是,当我在本地(localhost)服务器中运行它时,JSON 响应的长度是 55,这是正确的,它获得了我需要的所有事件。但是当我在我们的托管站点上尝试时,响应不一致,它与本地主机不同。对于第一次加载,我检查了长度响应,它返回了 55 个事件中的 32 个,然后我尝试再次重新加载它,然后响应增加到 39 等等。

这是我的本地主机响应。 [https://prnt.sc/rll4w0] 。这是 cpanel https://cdev.trilogycap.co/icedev/dynamic/civicore.php 上托管的文件.这是第一个回复https://prnt.sc/rll2rc .当我再次重新加载此页面时 https://prnt.sc/rll3fl

我的php文件函数流程是这样的。我有一个函数可以获取今年事件的所有 ID 并将其推送到全局数组。我已经删除了那里重复的 ID。所以现在我调用一个 multicurl 函数。

其中,对于每个ID,我将再次处理并调用API并构建它。然后,我将其推送到全局的 responseEvents 变量数组中,然后对其进行编码,以便能够生成有效的 JSON 文件。我在多 curl 中做了什么连接或错误的事情吗?它们是 php multi curl 的执行限制吗?或者 settimelimit(0) 会影响我的代码吗?

这是我的代码

<?php
//Use this so that when we run the curl it wont timeout when dealing with getting data
set_time_limit(0);
$API_KEY = "APIKEY";
$CURRENT_YEAR = date("Y");
$CURRENT_MONTH = date("m");
$CURRENT_DAY = date("d");
$events = getOpportunityYearly();//array of ID's removed duplicates [1,1,2,3,4,5 etc]
$noDuplicate = array_values(array_unique($events));//remove $event ID duplciates
$responseEvents = [];//this is the array to be returned as json to use

//pass noDuplicate array which holds event ID's
multiCurl($noDuplicate);
print_r(json_encode($responseEvents, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP |JSON_UNESCAPED_SLASHES ));

//returns an array of ID of events
function getOpportunityYearly(){
$eventArr = [];//pass eventsID here
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.com/voc/api/v3/data/opportunities_dates?key='.$GLOBALS['API_KEY'].'&fields=["startDate","endDate","opportunities_id"]&where={"whereType":"AND","clauses":[{"fieldName":"startDate","operator":">=","operand":"'.$GLOBALS['CURRENT_YEAR'].'-'.$GLOBALS['CURRENT_MONTH'].'-'.$GLOBALS['CURRENT_DAY'].'"},{"fieldName":"endDate","operator":"<=","operand":"'.$GLOBALS['CURRENT_YEAR'].'-12-31"}]}',
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",
));

//convert response to obj
$response = json_decode(curl_exec($curl));
curl_close($curl);
$eventsID = $response->records;
//print_r($eventsID);

//every opportunity object get its id and push to events array
foreach($eventsID as $opportunity){
array_push($eventArr,$opportunity->opportunities_id->value);
}
return $eventArr;
}

function multiCurl($eventArray){
// array of curl handles
$multiCurl = array();

// data to be returned
$result = array();

// multi handle
$mh = curl_multi_init();
$index = null;

foreach ($eventArray as $event) {
//$event are the ID per each event
$multiCurl[$event] = curl_init();
curl_setopt_array($multiCurl[$event], array(
CURLOPT_URL =>'https://api.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"=>[
"typeOfWork"=>$record->typeOfWork->displayValue,
"opportunityName"=> $record->opportunityName->displayValue,
"firstDateInOpportunity"=> $record->firstDateInOpportunity->displayValue,
"lastDateInOpportunity"=> property_exists($record, 'lastDateInOpportunity') ? $record->lastDateInOpportunity->displayValue : $record->firstDateInOpportunity->displayValue,
"startTime"=>$record->startTime->displayValue,
"endTime"=>$record->endTime->displayValue,
"physicalDifficulty"=>$record->physicalDifficulty->displayValue,
"minimumAge"=>$record->minimumAge->displayValue,
"location"=>$record->location->displayValue,
"state"=>$record->state->displayValue,
"city"=>$record->city->displayValue,
"county"=>$record->county->displayValue,
"campingAvailable"=>$record->campingAvailable->displayValue,
"groupsAllowed"=>$record->groupsAllowed->displayValue,
"registrationFormID"=>$record->registrationFormID->displayValue,
"cRQ_payment"=>$record->cRQ_payment->displayValue,
"paymentAmount"=>$record->paymentAmount->displayValue,
"photo"=>$record->photo->displayValue,
"displayToPublic"=>$record->displayToPublic->displayValue,
"latitude"=> property_exists($record, 'latitude') ? $record->latitude->displayValue : "0",
"longitude"=> property_exists($record, 'longitude') ? $record->longitude->displayValue : "0",
"photo"=>$record->photo->displayValue,
"description"=> $record->description->displayValue,
"additionalInformation"=> $record->additionalInformation->displayValue,
"summary"=> $record->summary->displayValue
]
];
array_push($GLOBALS["responseEvents"],$eve);
curl_multi_remove_handle($mh, $value);
}
}//foreach
curl_multi_close($mh);
}

?>


最佳答案

我认为不知何故,你的curl在输入这个foreach时没有检索所有信息foreach($multiCurl as $key=>$value)这可以解释为什么它在本地(快速响应)上可以,而不是在远程服务器上(网络时间,响应时间更长)。

你应该试试这个,这是我从 documentation 得到的并且应该等待所有连接正确结束。

do {
$status = curl_multi_exec($mh, $index);
if ($index) {
// Wait a short time for more activity
curl_multi_select($mh);
}
} while ($index && $status == CURLM_OK);

关于php - 从本地主机到实时站点的 Curl 响应不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60824418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com