如何在php中使用uber api进行价格估算

时间:2017-10-03 07:34:29

标签: php api uber-api

我第一次在我的项目中使用uber api。我希望通过超级api得到价格估算。我得到了这个结果:

  

{" message":"未提供身份验证。","代码":"未授权"}

我的源代码是:

$token = "acess token"; 


 $header=array(  
     "Authorization: Token $token",
     "Content-Type: application/json",
     "Accept-Language: en_US"

 // CALCULATE FAIR

 $url  =  "https://api.uber.com/v1.2/estimates/price?start_latitude=37.7752315&start_longitude=-122.418075&end_latitude=37.7752415&end_longitude=-122.518075"; 


 $curl=curl_init();
 curl_setopt($curl, CURLOPT_URL,$url);
 curl_setopt($curl,CURLOPT_HTTPAUTH,$header); 
 curl_setopt($curl, CURLOPT_USERPWD, "$token:");
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
 $output1 = curl_exec($curl);

  echo "<pre>";
 print_r($output1);

1 个答案:

答案 0 :(得分:2)

我修改了您的代码,请使用下面的代码..它正在运行

curl_setopt($curl,CURLOPT_HTTPAUTH,$header);应替换为curl_setopt($curl,CURLOPT_HTTPHEADER,$header);

请注意:永远不要在公共网站上放置这样的身份验证令牌,它可能会被误用,请重新生成新令牌并删除旧令牌。

<?php

$token = "your_auth_token";


$header = array(
    "Authorization: Token $token",
    "Content-Type: application/json",
    "Accept-Language: en_US");

// CALCULATE FAIR

$url = "https://api.uber.com/v1.2/estimates/price?start_latitude=37.7752315&start_longitude=-122.418075&end_latitude=37.7752415&end_longitude=-122.518075";


$curl    = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_USERPWD, $token);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output1 = curl_exec($curl);

echo "<pre>";
print_r($output1);