首页 > 解决方案 > 将 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,并区分特定的响应数据与额外参数的特定值相关联吗?

标签: phpcurl-multi

解决方案


$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'];

}

推荐阅读