question

youradrenalinefix avatar image
0 Likes"
youradrenalinefix asked

How To Extract access_token from Returned JSON String Using PHP

I wrote this title hoping to be able to help others with the response(s) to this question.

Im successfully returning an access_token JSON response using the code below but I'm not sure how to extract the access_token from the returned string with PHP.

function getOAuthCreds() {
$endpoint = 'https://api.ebay.com/identity/v1/oauth2/token';

$request = "grant_type=client_credentials&scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope";

$session = curl_init($endpoint);

curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $request);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$headers = [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Authorization: Basic My xxxx-Base64 Encoded Credentials-xxxx'
];

curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($session);
curl_close($session);
return $response;
}

Using the code above Im wanting to populate my 'X-EBAY-API-IAF-TOKEN like so: 'X-EBAY-API-IAF-TOKEN: getOAuthCreds() and any help will be greatly appreciated.

oauth2PHPshopping apiparsing json
· 1
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

For anyone using a similar approach here's how to extract the access_token https://www.w3schools.com/php/func_json_decode.asp

0 Likes 0 ·

0 Answers

· Write an Answer

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.