0

I am in the process of creating a website that ges informatin from an API that responds in JSON-format. I would like to execute the following command in PHP:

exec('curl -v -u {client_id}:{client_secret} -d code="{authorization_code}" -d redirect_uri={redirect_uri} -d grant_type=authorization_code https://api.basespace.illumina.com/v1pre3/oauthv2/token', $token);

This returns an array called $token which contains something of the following format:

Array ( [0] => {"error":"invalid_grant","error_description":"Authorization code is unknown for this application or an access token has already been issued for it."} )

In this example I would like to put the error and the error_description in seperate variables, for further processing. I already thought of using substr(), but the content of $token can vary, so I think that won't work... Is there another, more simple way to split the array?

2
  • json_decode didn't work ? Commented Jul 8, 2014 at 14:58
  • No, for some reason that doesn't work... Commented Jul 9, 2014 at 8:17

1 Answer 1

1
$arr = json_decode($token[0], true); //not a very indicative name for JSON, 'token'
$error = $arr['error'];
$error_desc = $arr['error_description'];
Sign up to request clarification or add additional context in comments.

5 Comments

Instead I would do ... $arr = json_decode($token[0]); ... since $token is an array. And drop the [0] from the rest.
Well it clearly does something - it turns a JSON-encoded string into an object or array. Do some debugging to see what it's returning for you. You can print_r() its result.
print_r($token) gives the output I gave in my question.
Also, if I try $arr = json_decode($token[0]);, everything after that is not executed and nothing is echod anymore
All right, I think I found the problem. I changed your first line to $arr = json_decode($token[0], true); and now it works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.