PHP get curl array results from inside a key value - php

I'm trying to get the access_token value from a curl response. The key value which is [0] renders out like so, {"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}.
However, I just need the value nested inside of access_token, which is 430f8a7d4f721a9e51e3558689ff28ec592923d2.
The output is rendered using this:
$cmd="curl -u testuser:123456 http://localhost/oauth2/server/token.php -d 'grant_type=client_credentials'";
exec($cmd,$result);
echo '<pre>'; print_r($result[0]); echo '</pre>';
How might go about doing this?
Btw, none of this is sensitive data. I'm just experimenting with oauth2.

Use the json_decode function on the results.
<?php
$json = '{"access_token":"430f8a7d4f721a9e51e3558689ff28ec592923d2","expires_in":3600,"token_type":"Bearer","scope":null}';
// Decode the JSON into an object
$decodedJson = json_decode($json);
$accessToken = $decodedJson->access_token;
var_dump($decodedJson,$accessToken);
// Decode the JSON into an array (note the true on the decode)
$decodedJson = json_decode($json,true);
$accessToken = $decodedJson['access_token'];
var_dump($decodedJson,$accessToken);

Encoding the json result, finds the subset.
$json = $result[0];
$json = json_decode($json, true);
echo $json['access_token'];

Related

Get values from JSON array using PHP

I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>

PARSE json string to json array (from GET)

i'm tring to parse url's json string to an json array.
ISSUE: json_decode = empty
QUESTION: Does anyone see what am i doing wrong?
MY STEPS:
tests from browser:
.....send.php?{"contactName":"name1"},{"contactName":"name2"}
my php code:
1. $url = $_SERVER['QUERY_STRING'];
2. $urlStringDecoded = urldecode($url);
echo urlStringDecoded result ok:
{"contactName":"name1"},{"contactName":"name2"}
3. $json = json_decode($urlStringDecoded, true);
RESULT EMPTY
echo("$json");
Seems like your JSON is invalid. Wrap your current string within [ ] Brackets.
Eg.
<?php
$url = $_SERVER['QUERY_STRING'];
//echo $url;
$urlStringDecoded = urldecode($url);
echo $urlStringDecoded;
$json = json_decode("[".$urlStringDecoded."]", true);
echo "<pre>";
print_r($json);
echo "</pre>";
?>
As you are trying to convert json string to json, you should provide right format. From you example it looks like you are passing , comma separated json objects whereas it should be an array. Add [] around your string and then try, in other words make it an array.

How to access individual values from JWTAuth JSON data

I am attempting to extract JSON values from the return of a JWTAuth token 'user' key but cannot figure out how to address the inner keys using php. I have the following code:
if(!$user = JWTAuth::parseToken()->authenticate()){
abort(401);
}
else{
return response()->json(compact('user'));
}
}
And when including the proper token in the header, I do receive the user info
{"user":{"id":2,"organization_name":"test corp","user_type":"administrator","created_at":"2016-05-13 17:26:20","updated_at":"2016-05-13 17:26:20","user_firstname":"Requester","user_lastname":"chester","user_emailaddress":"requester#test.com","remember_token":null,"user_id":"uid-7e3a0e15-c97b-44b1-885d-4370f4d1"}}
However, I cannot figure out a way to address the individual values of the keys inside of the "user" key. How can I get the value of say the "user_firstname" key?
Thanks.
UPDATE
Using the Log::Info function within laravel to log data a local log. The following:
$json = response()->json(compact('user'));
$arr = json_decode($json, true);
Log::Info(var_dump($arr));
Log::Info($arr["user"]["user_firstname"]);
returns NULL for both the var dump and the multidimensional array call
You might want to learn about json_encode and json_decode to convert json objects into an array and vice versa. See manual here - http://php.net/manual/en/function.json-decode.php
$array = json_decode($json, true);
var_dump($array);
echo $array["user"]["user_firstname"];
$json = '{"user":{"id":2,"organization_name":"test corp","user_type":"administrator","created_at":"2016-05-13 17:26:20","updated_at":"2016-05-13 17:26:20","user_firstname":"Requester","user_lastname":"chester","user_emailaddress":"requester#test.com","remember_token":null,"user_id":"uid-7e3a0e15-c97b-44b1-885d-4370f4d1"}}';
$arr = json_decode($json, true); //Converts JSON string into array
echo "<pre>";
print_r($arr);
echo "<pre>";
You could access array value :
echo $arr["user"]["user_firstname"]; //Prints Requester

How do I use JSON objects from Facebook with PHP?

http://developers.facebook.com/docs/reference/api/event
I'm trying to grab some values from a Facebook JSON even object in PHP. Namely, title of event, location, and people attending. Using the Graph API.
<?php
$jsonurl = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
How can echo the values from JSON output? I'm assuming it will be returned as an array.
Thank you!
If you're using PHP, you can use the PHP SDK to query the API. This means you can make calls like:
$user = $facebook->api('/someusername', array('fields' => 'id,first name,last_name ...'));
However, with your example, you could do the following:
$url = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$user = json_decode(file_get_contents($jsonurl));
echo $user['first_name'];
As if you use json_decode, it should decode the result into a native PHP array (or in same cases, an object).
Specify the second argument of true to json_encode to convert it to array and then you can print the output like this:
$json_output = json_decode($json, true);
echo '<pre>';
print_r($json_output);
echo '</pre>';
Now you can get specific item like this:
echo $json_output['title'];

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories