Search code examples
phplyft-api

Is a variable allowed in authorization bearer in place of a token?


I have a variable in place of a token key. When I use the variable the response is blank. When I use a token key it returns a response. I've made sure that the other variables I set weren't the problem. The problem variable is the one I have labeled $token_json

My website is https://geolyft.com

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php
$ltt = $_POST['lat'];
$lng = $_POST['lng'];
$token_url = "https://geolyft.com/authentication.php";
$token_json = file_get_contents($token_url);
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.lyft.com/v1/drivers?lat=$ltt&lng=$lng",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer $token_json",
    "cache-control: no-cache",
    "postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>
</body>
</html>

The difference is here

CURLOPT_HTTPHEADER => array(
    "authorization: Bearer gAAAAABYXNVPV4B9AEY2bHRZDhB_jw6-hj4ptvUttm45jqroJN0lV5QSZGd-5AL0jrVKod4R5J0Sdxam1Y2nduEO_Lvud8fFGnqAN7shjzgcl9RmLQN5WljEJ9us-Y41_qGr7HT10EH8acseSt-mdO7Dp9MeYaFMkcjIP-IdrIL9uVNll-JypwQgftZ1Bum7gCtQEgpqjGeRuwE4YwE6rLPFALUgZVWNlg==",
    "cache-control: no-cache",
    "postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
  ),
));

I would rather have this instead of a long token key but with this setup the response I get is blank and I've been stumped for a while now and I can't seem to find an answer perhaps I'm out of my league. Any help is appreciated

CURLOPT_HTTPHEADER => array(
    "authorization: Bearer $token_json",
    "cache-control: no-cache",
    "postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
  ),
));

Solution

  • The answer to my question was to put the authentication curl request before other curl requests.

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.lyft.com/oauth/token",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\nclient_credentials\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"scope\"\r\n\r\npublic\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic a0JFTE1Oak9QQWY2Om1VZVdwc1lRS0ExUWdfS1dnYTN1OHlHajQxQ3E4LV9X",
        "cache-control: no-cache",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
        "postman-token: f5d6e662-1663-e577-2c0d-705f03275fa8"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    $json = json_decode($response, true);
    $token = $json['access_token'];