I have a small PHP curl script that's accessing an API URL. This is the last part of it:
$response = curl_exec($curl);
$err = curl_error($curl);
$re = json_decode($response, true);
var_dump($re);
$item = $re['access_token'];
var_dump($item);
This is what is contained in the $re variable dump:
array(4) { ["auth"]=> array(4) { ["access_token"]=> string(213) "000000000000000000000" ["expires_in"]=> string(7) "2592000" ["issued"]=> string(21) "7/10/2018 11:56:41 PM" ["expires"]=> string(21) "6/11/2018 11:56:41 PM" } ["httpStatusCode"]=> int(200) ["httpStatusMessage"]=> string(11) "RESPONSE.OK" ["message"]=> string(2) "OK"}
And the dump for $item is NULL. I can't seem to grab the access_token value from within the returned array.
I've also tried this:
$item = $re['auth'][0]['access_token'];
But it also comes up NULL.
Any ideas why this wouldn't be working?
Thanks
$re = json_decode($response, true);
This line returns associative array.
So if you could try
$item = $re['auth']['access_token'];
it should return the value you are looking for.
Related
i have this:
{"sliders":{"c1":{"content":[{"title":1,"content_type":"image_content"}]}}}
i can get the title using the code below:
$decoded = json_decode($list[$i]['info'],true);
$json = $decoded['sliders']['c1']['content'][0]);
$x = $json['title'];
echo $x;
when i am trying to get the content_type...
$y = $json['content_type'];
echo $y;
...then it shows me undefined index error..why this happens?
First of all: The statement…
$json = $decoded['sliders']['c1']['content'][0]);
…should have given a syntax error for the excessive right bracket )
A var_dump of $decoded shows this:
object(stdClass)#1 (1) {
["sliders"]=>
object(stdClass)#2 (1) {
["c1"]=>
object(stdClass)#3 (1) {
["content"]=>
array(1) {
[0]=>
object(stdClass)#4 (2) {
["title"]=>
int(1)
["content_type"]=>
string(13) "image_content"
}
}
}
}
}
So, the entire variable $decoded is an object of objects.The object content is an array of one (in this case) object: content[0].
Therefore you would access the two items like this - using the object and array notation where appropriate:
echo $decoded->sliders->c1->content[0]->title;
echo $decoded->sliders->c1->content[0]->content_type;
OR
$json = $decoded->sliders->c1->content[0];
echo $json->title;
echo $json->content_type;
…which both would give:
1
image_content
Here's the relevant part of my PHP:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['identities[0].user_id'];
The $response looks good, as does $stuff2. What I'm trying to get is the value for user_id and store as a variable I can use in PHP ($user_id). If I var_dump $stuff2, I get something like this:
array(2) {
["nickname"]=>
string(3) "jmr"
["identities"]=>
array(1) {
[0]=>
array(2) {
["user_id"]=>
string(24) "5458fec4aa0395931002fe71"
["connection"]=>
string(32) "Username-Password-Authentication"
}
}
}
If I do this:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['nickname'];
then I get "jmr" no problem, but I can't figure out how to get that "user_id" buried in that second array within the array.
Do a print_r($stuff2); and you will see the array is just an ordinary multi-dimensional array. I'm not sure why you have that convoluted key, but you access it just like an ordinary multi-dimensional array:
$user_id = $stuff2['identities'][0]['user_id'];
I've been trying for hours now to get the title field from the json code. Below is my php code
$search = $_GET['search'];
$new = str_replace(' ', '+', $search);
$url = "http://api.themoviedb.org/3/search/movie?api_key=###&query=".$new;
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$title = $json_data->title;
echo $title;
this is the var dump of the json
array(4) { ["page"]=> int(1) ["results"]=> array(1) { [0]=> array(10) { ["adult"]=> bool(false) ["backdrop_path"]=> string(32) "/4uJZvmEMX6Z6ag3bzym5exLY9wI.jpg" ["id"]=> int(65) ["original_title"]=> string(6) "8 Mile" ["release_date"]=> string(10) "2002-11-08" ["poster_path"]=> string(32) "/dXzTrKwpbLpCqn8O70FUUhNbYQT.jpg" ["popularity"]=> float(3.792332418578) ["title"]=> string(6) "8 Mile" ["vote_average"]=> float(6.2) ["vote_count"]=> int(185) } } ["total_pages"]=> int(1) ["total_results"]=> int(1) }
the error i keep getting is Notice: Trying to get property of non-object
any help would be greatly appreciated.
$json_data = json_decode($json, true);
will return an array not object
so you need to use as
$json_data["title"];
NOTE : Your json decoded array is nested so you may need to use as in your case.
$json_data["results"][0]["title"];
Or better loop through and get the desired data.
I just started using PHP and I THINK this might help you... but i'n not sure
foreach($json_data as $key => $value){
if($key == "results"){
$json_data2 = $value;
}
}
foreach($json_data2 as $key => $value){
if($key == "original_title"){
$title = $value;
}
}
echo $title;
How can I get out of this likeCount? Thanks cause I have no idea how to do it
https://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc
The array
object(stdClass)#1 (2) {
["apiVersion"]=>
string(3) "2.1"
["data"]=>
object(stdClass)#2 (19) {
["id"]=>
string(11) "f4LxBKN9ank"
["uploaded"]=>
string(24) "2014-01-26T02:34:24.000Z"
["title"]=>
string(25) "League of Legends : Worth"
["content"]=>
object(stdClass)#5 (3) {
["5"]=>
string(74) "https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata"
["1"]=>
string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
["6"]=>
string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
}
["duration"]=>
int(78)
["aspectRatio"]=>
string(10) "widescreen"
["rating"]=>
float(4.921824)
["likeCount"]=>
string(5) "14147"
["ratingCount"]=>
int(14429)
["viewCount"]=>
int(678017)
}
}
You can do like that.
<?php
$data = #file_get_contents("http://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc");
$realdata = json_decode($data);
$likecount = $realdata->data->likeCount;
// Sample data
$data = '{"apiVersion":"2.1","data":{"id":"f4LxBKN9ank","uploaded":"2014-01-26T02:34:24.000Z","updated":"2014-03-01T16:48:02.000Z","uploader":"videogamedunkey","category":"Comedy","title":"League of Legends : Worth","description":"Allow 3-5 weeks for your burrito to arrive.\n\nhttps://www.youtube.com/watch?v=YxeOLw1npuo&list=FLsvn_Po0SmunchJYOWpOxMg&index=1","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/default.jpg","hqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=f4LxBKN9ank&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=f4LxBKN9ank"},"content":{"5":"https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata","1":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":78,"aspectRatio":"widescreen","rating":4.921824,"likeCount":"14147","ratingCount":14429,"viewCount":678049,"favoriteCount":0,"commentCount":1497,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}';
// Decode $data into an array
$json = json_decode($data, true);
// Get the like count from data/likeCount in the array
$likeCount = $json["data"]["likeCount"];
// Display the likeCount
echo $likeCount; // 14147
$json = json_decode($data, true) decodes the data in $data into an array called $json. This array has a sub-array whose key is "data", and this sub-array contains the like count under the key "likeCount". Therefore, the like count can be accessed by chaining indexes together: $likeCount = $json["data"]["likeCount"])
$url2 = "http://www.website.com/test.json";
$json2 = file_get_contents($url2);
$data2 = json_decode($json2);
foreach($data2 as $mydata2) {
$product_discount = $mydata2->applied_discounts;
var_dump($product_discount);
}
This is returning:
array(1) {
[0]=> object(stdClass)#2 (2) {
["id"]=> string(6) "coupon"
["amount"]=> float(9.99)
}
}
I want to return just the amount of "9.99"
I tried $product_discount[0]['amount'], but that doesn't seem to be right??
It's an object, so you need the following syntax:
$product_discount = $mydata2->applied_discounts[0]->amount;
However, if you want an array instead, you could set json_decode()'s second parameter as TRUE:
$data2 = json_decode($json2, TRUE);
You want to do this:
$product_discount = $mydata2->applied_discounts[0]->amount;
It's an object, not an array. Try...
$product_discount[0]->amount