parsing data from json php get image src - php

I want to parse data from JSON Instagram without API,
a result like this
Array ( [nodes] => Array ( [0] => Array ( [__typename] => GraphVideo [id] => 1532294365652508146 [comments_disabled] => [dimensions] => Array ( [height] => 750 [width] => 750 ) [gating_info] => [media_preview] => ACoq6aiionJGTQBLRWXLOwJySuFyATjv16j2496dbu8mQWJwAVPqp6H69jQBpUUg6UtABWLfXslvkjBGcAEVtVxOoO09w69FRj9Bj+poASC5kklJkO7cDn8fT2rTsJminEJ5XG36dx+XeseAHf8APkg8Z7EfX04pxuvs0xaMDAOQD0zigDuaWooX8yNXP8Sg/mM1LQAVEYIySSq5PX5Rz9eKlooAiEEYGAqgfQU020J5KIf+Aj/Cp6KAEAAGBwBS0UUAf//Z [owner] => Array ( [id] => 6860189 ) [thumbnail_src] => https://scontent.cdninstagram.com/t51.2885-15/s640x640/e15/18888588_1753665424925670_2681598092939100160_n.jpg [is_video] => 1 [code] => BVDzZtsDN3y [date] => 1496883744 [display_src] => https://scontent.cdninstagram.com/t51.2885-15/e15/18888588_1753665424925670_2681598092939100160_n.jpg [video_views] => 2304729 [caption] => #2U Friday #romeestrijd [comments] => Array ( [count] => 18485 ) [likes] => Array ( [count] => 985087 ) ) [1] => Array ( [__typename] => GraphVideo [id] => 1532284690492268192 [comments_disabled] => [dimensions] => Array ( [height] => 750 [width] => 750 ) [gating_info] => [media_preview] => ACoq6aikNU7ycwwF1wDwAT0GSBn8KALlGa5u21KSGRVmbzI3ON5GME9MHuP89q6OgB1FJS0AIaytVRntG29sH64Nap6Vg6xKUtlXOGY9B0IHJ+vOPzoA56SVnQZ9evoR6eldtZzieJXyMkDODnBxXCNOzrtOPr3rZ0ViHCDo27d+XH9KAOsoqpZwPBHskbeck556enOat0AJVG606O6ADlgF6AEY+vIP0/8A1VfooAw/+Eft/V/zH/xNXbPTorMkoWJIx8xB/kBV+igAooooA//Z [owner] => Array ( [id] => 6860189 ) [thumbnail_src] => https://scontent.cdninstagram.com/t51.2885-15/s640x640/e15/18947831_1799767330352720_4657693158346326016_n.jpg [is_video] => 1 [code] => BVDxM6_j56g [date] => 1496882591 [display_src] => https://scontent.cdninstagram.com/t51.2885-15/e15/18947831_1799767330352720_4657693158346326016_n.jpg [video_views] => 3116560 [caption] => #2U Friday #sarasampaio [comments] => Array ( [count] => 28703 ) [likes] => Array ( [count] => 1272010 ) ) [2] => Array ( [__typename] => GraphImage [id] => 1532046157831653372 [comments_disabled] => [dimensions] => .....
how to I can get a display_src result in foreach loop
$data = json_decode($response, true);
if ($data === null) {
break;
}
$media = $data['user']['media'];
$found += count($media['nodes']);
foreach ($media as $dataa){
echo $dataa->nodes->display_src;
}
I try it but I do not get result.

Related

How to loop over multi dimensional Instagram array and get the url of all images?

I want to loop over the array below and echo out every low_resolution url.
I have tried running a foreach loop over this but since it is multi dimensional I am not sure how to access the url value. (Not used to working with multi dimensional arrays)
PHP:
public function getInstagramImages() {
$url = "https://api.instagram.com/v1/users/31008104/media/recent?access_token=31008104.02ed65e.4130309107d34e2cb220d0ef62c2e86d";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($json, true);
print_r($json_array);
}
The array:
ArrayArray ( [pagination] => Array ( ) [meta] => Array ( [code] => 200 ) [data] => Array ( [0] => Array ( [attribution] => [tags] => Array ( ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => Amaro [created_time] => 1399119582 [link] => https://www.instagram.com/p/niND4jsISQi7hJjODg-Z6Mm1MaMrY9nOIfiaQ0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1399119582 [text] => Insane mall! [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 712189132938446543 ) [user_has_liked] => [id] => 712189132569347216_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) ) [1] => Array ( [attribution] => [tags] => Array ( [0] => tailored ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => 1977 [created_time] => 1398926862 [link] => https://www.instagram.com/p/ncdeeosIU0UR6oYZoy8YMuwAGsM4PPDtW1xUo0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1398926862 [text] => Getting suited up! #tailored [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 710572479314167115 ) [user_has_liked] => [id] => 710572479045731636_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) )
Screenshot of array:
FYI the array goes on further than this. Just showing this part to keep it as simple as possible.
You can do something like this,
foreach($json_array['data'] as $images){
echo $images['images']['low_resolution']['url'];
}
This will give you all the low_resolution urls from each data array.
I tried with following code to get all image titles & urls.
if(is_array($user_recent_data->data)) {
foreach($user_recent_data->data as $feed_data) {
if(!empty($feed_data->caption))
{
$mediaTitle = $feed_data->caption->text;
}
else
{
$mediaTitle = '';
}
?>
<p><?php echo "<img src='".$feed_data->images->thumbnail->url."' style='width:135px; border:1px solid #ccc; padding:3px;'/>";?></p>
<?php
}
}
?>
$user_recent_data is the veriable where instagram array is collected.
What you need is a recursive function which looks for the index of url like this one:
function find_urls($array){
$urls = array();
foreach ($array as $item){
if (is_array($item)){
if(isset($item['url']){
$urls[] = $item['url'];
}
array_merge($urls,find_urls($item));
}
}
return $urls;
}
This should find any index called url in any array with any structure ;)

Converting a php object into associative array

I am trying to read the following php object result of an api call into an associative array. Has someone done this before or can otherwise help me?
I have tried object var dump, and array casting, but this just doesnt seems to work. I know I am doing something fundamentally wrong. Can someone help?
Many thanks.
Google_Service_Directory_Users Object
(
[etag] => "sfgsfgdsfgsdfgjkdjgfd"
[kind] => admin#directory#users
[nextPageToken] =>
[triggerEvent] =>
[usersType:protected] => Google_Service_Directory_User
[usersDataType:protected] => array
[collection_key:protected] => items
[modelData:protected] => Array
(
[users] => Array
(
[0] => Array
(
[kind] => admin#directory#user
[id] => 7642341239423
[etag] => "jasdfjshwer43537345fsdfs"
[primaryEmail] => info#example.com
[name] => Array
(
[givenName] => Info -
[familyName] => Example
[fullName] => Info - Example
)
[isAdmin] =>
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-07-29T08:46:28.000Z
[creationTime] => 2014-07-29T08:31:56.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => info#example.com
[primary] => 1
)
)
[nonEditableAliases] => Array
(
[0] => info#example.com.test-google-a.com
)
[customerId] => fsdfdd4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
[1] => Array
(
[kind] => admin#directory#user
[id] => 3895729453245
[etag] => "fsajdfd64hkj4534h5k3454"
[primaryEmail] => user#example.com
[name] => Array
(
[givenName] => User
[familyName] => Name
[fullName] => User Name
)
[isAdmin] => 1
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-08-26T09:05:49.000Z
[creationTime] => 2012-09-16T08:55:26.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => support#example.com
)
[1] => Array
(
[address] => help#example.com
)
)
[customerId] => fsdafwr4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
)
)
[processed:protected] => Array
(
)
)
First solution:
$array = json_decode(json_encode($nested_object), true);
Second solution:
function object_to_array($data) {
if (is_array($data) || is_object($data)):
$result = array();
foreach ($data as $key => $value)
$result[$key] = object_to_array($value);
return $result;
endif;
return $data;
}
both searched from the internetz

How to sort object array by value deep inside array

I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}

retrieving data from a php object

I am trying to access data inside a php object. I am confused about what the exact stynax is. Here is the output of the object:
stdClass Object ( [vid] => 10 [uid] => 1 [title] => Beachhead Don [log] =>
[status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] =>
10 [type] => product [language] => und [created] => 1357668665 [changed] => 1358198386
[tnid] => 0 [translate] => 0 [revision_timestamp] => 1358198386 [revision_uid] => 1
[model] => B 3000 [list_price] => 0.00000 [cost] => 0.00000 [sell_price] => 1.00000
[weight] => 0 [weight_units] => lb [length] => 0 [width] => 0 [height] => 0
[length_units] => in [pkg_qty] => 1 [default_qty] => 1 [unique_hash] =>
2eec0fcc8483f3a3541870fb24223296 [ordering] => 0 [shippable] => 0 [price] => 1.00000
[body] => Array ( [und] => Array ( [0] => Array ( [value] => Description info
))))))))))))))))))))))))))))))))))))))0 [summary] => [format] => filtered_html
[safe_value] =>Description info ))))))))))))))))))))))))))))))))))))))0[safe_summary]
=> ) ) ) [uc_product_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 11
[alt] => [title] => [width] => 90 [height] => 120 [uid] => 1 [filename] =>
beachhead_don.jpg [uri] => public://beachhead_don.jpg [filemime] => image/jpeg
[filesize] => 14342 [status] => 1 [timestamp] => 1357668665 [rdf_mapping] => Array ( )
) ) ) [field_author] => Array ( [und] => Array ( [0] => Array ( [value] => John B.
Romesier [format] => [safe_value] => John B. Romesier ) ) ) [field_publisher] => Array
( [und] => Array ( [0] => Array ( [value] => Unknown [format] => [safe_value] =>
Unknown ) ) ) [field_release_date] => Array ( [und] => Array ( [0] => Array ( [value]
=> Mar 2012 [format] => [safe_value] => Mar 2012 ) ) ) [field_number_of_pages] => Array
( [und] => Array ( [0] => Array ( [value] => 123 ) ) ) [field_catagory] => Array (
[und] => Array ( [0] => Array ( [tid] => 4 ) ) ) [field_book_type] => Array ( [und] => Array ( [0] => Array ( [tid] => 1 ) ) ) [field_isbn] => Array ( ) [field_about_the_author] => Array ( [und] => Array ( [0] => Array ( [value] => Beachhed Don authoer............................ [format] => [safe_value] => Beachhed Don authoer............................ ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1357668665 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
I am trying to retrive the [value] of Description info ))))))))))))))))))))))))))))))))))))))0
I tried this
<?php
$node = menu_get_object(); //drupal code
if ( !empty($node) ) {
print $node ->body=>und=>0=>value;
}?>
and got an error about the '=' sign. What is the proper way to get my data?
[body] and [und] are arrays, so access them like this:
echo $node->body['und'][0]['value'];
It should be:
$node->body['und'][0]['value']
'Body' and 'und' are Arrays and need to be accessed with array syntax([]).
Accessing these values directly is sometimes frowned upon in drupal. You may want to look into: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_render/7

Youtube API PHP json decode

I'm trying to search youtube and grab the videos video ID and title.
In the bellow example I am searching for "youtube":
<?php
$url="http://gdata.youtube.com/feeds/api/videos?q='youtube'&format=5&max-results=2&v=2&alt=jsonc";
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
echo '<pre>';
print_r("query results:");
print_r($json_output);
'</pre>';
foreach ( $json_output->data as $data ){
echo "{$data->id}";
echo "{$data->title}";
}
?>
Here is the json output for the above query:
query results:stdClass Object
(
[apiVersion] => 2.1
[data] => stdClass Object
(
[updated] => 2012-03-04T20:19:06.314Z
[totalItems] => 1000000
[startIndex] => 1
[itemsPerPage] => 2
[items] => Array
(
[0] => stdClass Object
(
[id] => IpSYwvzKukI
[uploaded] => 2009-03-14T08:17:36.000Z
[updated] => 2012-02-29T10:51:35.000Z
[uploader] => kenbedict009
[category] => Music
[title] => one of the funniest kid in youtube!!
[description] => he is a 3 years old korean,this kid makes me laugh
[tags] => Array
(
[0] => rin on the rox
[1] => charice pempengco
[2] => joeydiamond
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/default.jpg
[hqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=IpSYwvzKukI&feature=youtube_gdata_player
[mobile] => http://m.youtube.com/details?v=IpSYwvzKukI
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/IpSYwvzKukI?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
[duration] => 344
[rating] => 4.6304307
[likeCount] => 23419
[ratingCount] => 25803
[viewCount] => 9848083
[favoriteCount] => 15390
[commentCount] => 16074
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => moderated
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
[1] => stdClass Object
(
[id] => PPNMGYOm1aM
[uploaded] => 2011-06-23T16:35:33.000Z
[updated] => 2012-02-29T13:15:47.000Z
[uploader] => shakeitupvevo
[category] => Music
[title] => "Watch Me" from Disney Channel's "Shake It Up"
[description] => To download Watch Me visit www.smarturl.it Performed by Bella Thorne and Zendaya
[tags] => Array
(
[0] => Bella
[1] => Thorne
[2] => Zendaya
[3] => Cast
[4] => of
[5] => Shake
[6] => It
[7] => Up:
[8] => Break
[9] => Down
[10] => Watch
[11] => Me
[12] => Walt
[13] => Disney
[14] => Soundtrack
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/default.jpg
[hqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=PPNMGYOm1aM&feature=youtube_gdata_player
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/PPNMGYOm1aM?version=3&f=videos&app=youtube_gdata
)
[duration] => 193
[aspectRatio] => widescreen
[rating] => 4.7312055
[likeCount] => 145059
[ratingCount] => 155509
[viewCount] => 48201555
[favoriteCount] => 69981
[commentCount] => 81938
[status] => stdClass Object
(
[value] => restricted
[reason] => limitedSyndication
)
[restrictions] => Array
(
[0] => stdClass Object
(
[type] => country
[relationship] => deny
[countries] => DE
)
)
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => allowed
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
)
)
)
Im trying to echo out [id] and [title]. Any idea what I am doing wrong?
Thanks!
Change line 9 to:
foreach ( $json_output->data->items as $data ){
You should get first items as an Objects Array:
$items = $json -> data -> items;
So try foreach now:
foreach ( $items as $item ){
echo "{$item->id}";
echo "{$item->title}";
}
I hope it's clear now.

Categories