使用适当的数组值映射curl_multi值

时间:2018-05-14 06:42:07

标签: 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?有什么方法可以发送额外的参数来卷曲,并区分特定的响应数据是否与额外参数的特定值相关联?

1 个答案:

答案 0 :(得分:0)

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

}