varsha varsha - 1 year ago 120
PHP Question

How to use uber api for price estimate in php

i am using uber api in my project for the first time ...i want get price estimate through uber api....i am getting this result " {"message":"No authentication provided.","code":"unauthorized"}"..
Please help me ..

My source code is this :

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

Answer Source

I have modified your code, please use below code.. it is working

curl_setopt($curl,CURLOPT_HTTPAUTH,$header); should be replaced with curl_setopt($curl,CURLOPT_HTTPHEADER,$header);

Please note: never ever put your Authentication tokens like this on a public website, it can be misused, please re-generate a new token and delete the old one.

<?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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download