0

I have produced some code that requests a bearer token from an endpoint and adds a new key with a datetime value with seconds that get added to it. This code also produces a fatal error as in the title of this question on line 66:

function requestBearerToken()
{
    global $username, $password;
    $base_url = 'https://example-url.com/endpoint';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ($base_url));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Basic ' . base64_encode("$username:$password"),
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    //$data = curl_exec($ch);
    curl_close($ch);
    $data['expires_on'][0] = date("d-m-Y H:i:s", (time() + 580));
    file_put_contents("bearer.json", $data);
    $pre_result = json_decode($data, true);
    $token = $pre_result;
    print_r($token);
}

The reponse body, $data, is as follow:

{
  "access_token": "<access_token>",
  "token_type": "Bearer",
  "expires_in": 299,
  "scope": "<scopes>"
}
Luuc
  • 99
  • 1
  • 9
  • Currently the original `$data` assignment is commented out, so `$data` doesn't exist when you're trying to access `$data['expires_on'][0]`. In your data object, you have `expires_in`, not `expires_on`, and that's an integer, not an array, so you can't treat it as an array. – aynber Dec 29 '22 at 14:24
  • Sharp thinking, I removed the comments but the error still gets thrown. – Luuc Dec 29 '22 at 14:39
  • Of course it does. Did you read the rest of my comment? `$data['expires_in']` is an integer, you can't treat it as an array. There is no `$data['expires_on']` currently. If you want that as an array, you'll have to create it with `$data['expires_on'] = [date("d-m-Y H:i:s", (time() + 580))];` – aynber Dec 29 '22 at 14:43
  • I have tried this so that a new key is added for 'expires_on'. The same error is being thrown. – Luuc Dec 29 '22 at 14:53

0 Answers0