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

How to use uber api for price estimate in php

我是第一次在我的项目中使用优步 api。我想通过 uber api 获得估价。我得到这个结果:

{"message":"No authentication provided.","code":"unauthorized"}

我的源代码是这样的:

$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);

我已经修改了你的代码,请使用下面的代码..它正在工作

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

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

<?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);