php - 将 curl_multi 值映射到适当的数组值
问题描述
我有一个这样的数组:-
$venueLocations = array(
[0] => array(
'id' => '3Bheip34nbh503k3k30',
'latitude' => '22.45',
'longitude' => '88.45'
),
[1] => array(
'id' => '3Bheip34nbherf456gh3k3k30',
'latitude' => '22.455',
'longitude' => '88.457'
),
[2] => array(
'id' => '3fcD34Gp3nbherf56gh3k3k30',
'latitude' => '22.455',
'longitude' => '88.457'
)
);
我想使用 curl 访问 Uber API 以获取前往该地点的预计时间。
我正在使用 multi_curl_exec 同时运行卷曲,而不是一个接一个。这是我的代码:-
public function simultaneousUberTimeEstimateCall($location, $venueLocation){
$teList = array();
$placeList = array();
$multiCurl = array();
$result = array();
$mh = curl_multi_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
foreach ($venueLocation as $i => $vloc)
{
$url = self::$uberApiTimeEstimate .'?start_latitude=' . $location['latitude'] . '&start_longitude=' . $location['longitude'] . '&end_latitude=' . $vloc['latitude'] . '&end_longitude=' . $vloc['longitude'];
$multiCurl[$i] = curl_init();
curl_setopt($multiCurl[$i], CURLOPT_URL, $venueDetailApiUrl);
curl_setopt($multiCurl[$i], CURLOPT_HTTPHEADER, $headers);
curl_setopt($multiCurl[$i], CURLOPT_HEADER, 0);
curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $multiCurl[$i]);
}
$index = null;
do
{
curl_multi_exec($mh,$index);
} while($index > 0);
// get content and remove handles
foreach($multiCurl as $k => $ch)
{
$result[$k] = json_decode(curl_multi_getcontent($ch), TRUE);
curl_multi_remove_handle($mh, $ch);
}
// close
curl_multi_close($mh);
foreach($result as $key => $estimatedTime)
{
if(!empty($estimatedTime['times'][0]['estimate']))
{
$te['uber_eta'] = intval($estimatedTime['times'][0]['estimate'])/60;
}
else
{
$te['uber_eta'] = 'N/A'
}
$teList[] = $te;
}
return $teList;
}
问题出在这里:-
foreach($result as $key => $estimatedTime)
{
if(!empty($estimatedTime['times'][0]['estimate']))
{
$te['uber_eta'] = intval($estimatedTime['times'][0]['estimate'])/60;
}
else
{
$te['uber_eta'] = 'N/A'
}
$teList[] = $te;
}
我希望 $teList 是这样的:-
$teList = array(
[0] => array(
'id' => '3Bheip34nbh503k3k30',
'uber_eta' => '8'
),
[1] => array(
'id' => '3Bheip34nbherf456gh3k3k30',
'uber_eta' => '9'
),
[2] => array(
'id' => '3fcD34Gp3nbherf56gh3k3k30',
'uber_eta' => '3'
)
);
如何区分哪个curl值对应哪个id?有什么方法可以将额外的参数发送到 curl,并区分特定的响应数据与额外参数的特定值相关联吗?
解决方案
$venueLocation 中的密钥应该与分配 curl 句柄的顺序相同。因此,我很确定将为响应保留密钥顺序。因此,只需将 $venueLocation 数组中的 id 按键添加到结果数组中即可。
尝试这个:
foreach($result as $key => $estimatedTime){
if(!empty($estimatedTime['times'][0]['estimate'])){
$te['uber_eta'] = intval($estimatedTime['times'][0]['estimate'])/60;
}else{
$te['uber_eta'] = 'N/A';
}
$teList[$key]['uber_eta'] = $te['uber_eta'];
$teList[$key]['id'] = $venueLocation[$key]['id'];
}
推荐阅读
- python - 对于每个循环,更新变量
- apache - 如何正确使用 Apache 的 mod_env 模块来修改 PATH?
- c# - HRESULT 异常:尝试读取 Airpod pro 电池时出现 0xD0000033
- python - python中的多个条件if语句语法
- azure - Azure Blob 容器文件下载
- eggplant - Turbo Capture Mode - 无法识别点击进入另一个字段
- kubernetes - 带有`gcloud auth login`和`gcloud get-credentials`的GKE kubectl错误:无法连接到服务器:x509:证书由未知机构签名
- r - 标题:将变量值传递给 R 中的函数
- c# - 阅读器内的 Firebird 更新
- c# - i am trying to figure out how to count only the nodes width a specific value in xml