估计乘车价格(优步)

时间:2018-02-19 12:32:10

标签: php uber-api

好吧,我正在尝试使用api为超级乘车创建价格请求,但我没有成功......我不明白代码中发生的事情是什么错误,我会打印并将代码放在这里,如果你能帮助我,我会很感激......

这是错误:

  

注意:未定义的索引:第20行的C:\ xampp \ htdocs \ index.php中的代码

     

注意:未定义的变量:C:\ xampp \ htdocs \ index.php中的fields_string   第11行{"错误":" invalid_grant"}

我的index.php:

<?php

function fetchUrl($url,$code){
    $fields = array(
        'client_id' => "ibhY[.....]BI6",
        'client_secret' => "vMq[.....]6-ga[.....]rqU-[.....]",
        'grant_type' => "authorization_code",
        'redirect_uri' => "http://localhost:80/request.php",
        'code' => $code
    );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch);
    curl_close($ch);
}
$data = fetchUrl('https://login.uber.com/oauth/v2/token', $_GET['code']);
$data = json_decode($data);
print_r($data);

我的request.php

<?php

$token = "KA.eyX[....................................]5jk";


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

image from my uber control panel

1 个答案:

答案 0 :(得分:0)

以下错误:

Notice: Undefined index: code in C:\xampp\htdocs\index.php on line 20

指的是以下代码行:

$data = fetchUrl('https://login.uber.com/oauth/v2/token', $_GET['code']);

这表明授权代码未传递到您的index.php脚本中。

您需要获得用户的授权才能接收代码。有关如何执行此操作的文档可在此处找到:

https://developer.uber.com/docs/riders/guides/authentication/user-access-token#step-1-user-authorizes-your-app

获得此代码后,您将能够代表用户请求和访问令牌并提出请求(例如,获取价格估算值)。