Search code examples
phpapiuber-api

How to use uber api for price estimate in php


I am using uber api in my project for the first time. I want to get a price estimate through the uber api. I am getting this result:

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

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

Solution

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