I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). I've been able to call the service, retrieve the json body, and use json_decode as follows to create an array:
$insightList = json_decode($guzzleResponse ->getBody(), true);
However, I can't figure out how to get access to the field I'm interested in. I've searched for solution on the web and tried a few approaches that looked promising, but when I tried to integrate them, I couldn't get them to work. I'm a bit of a PHP novice, so if something doesn't work I'm not sure how to proceed.
The json structure is quite complex, with three top-level arrays - search, tweets, and next. I'm interested in the second of these, tweets. It's a pretty complex array - there is one entry per tweet. The field I want right now is tweets.cde.message.body. You can find the full schema here: https://cdeservice.eu-gb.mybluemix.net/rest-api/#!/messages/getTweets
This is the code I have so far:
foreach($insightList as $cde) {
foreach($cde as $message) {
$insight = $message['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
}
Here's a print_r of $insightList:
[search] => Array (
[results] => 28
[current] => 28
)
[tweets] => Array (
[0] => Array (
[cde] => Array (
[author] => Array (
[gender] => male
[parenthood] => Array (
[isParent] => unknown
)
[location] => Array (
[country] =>
)
[maritalStatus] => Array (
[isMarried] => unknown
)
)
[content] => Array (
[sentiment] => Array (
[evidence] => Array ( )
[polarity] => NEUTRAL
)
)
)
[message] => Array (
[postedTime] => 2015-01-13T09:42:16.000Z
[verb] => share
[link] => http://twitter.com/zWDOM/statuses/554936456477933569
[generator] => Array (
[displayName] => Twitter Web Client
[link] => http://twitter.com
)
[body] => RT #VisualSuccess: "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com #zWDOM #Rocket #JohnKnutson_IBM
[favoritesCount] => 0
[objectType] => activity
[actor] => Array (
[summary] => Seit über 25 Jahren im Mainframebereich, derzeit als Senior Consultant und IT Architekt für zEnterprise und Projektmanager beim IBM BP Cancom in Köln
[image] => https://pbs.twimg.com/profile_images/424202233463308288/XQquUcnh_normal.jpeg
[statusesCount] => 2309
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => zWDOM
[displayName] => Willi Domroese
[postedTime] => 2009-12-17T01:39:25.000Z
[link] => http://www.twitter.com/zWDOM
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => medium
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 77
[1] => 99
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 3
[1] => 17
)
[screen_name] => VisualSuccess
[id_str] => 213337792
[name] => Predrag Gasic
[id] => 213337792
)
[1] => Array (
[indices] => Array (
[0] => 101
[1] => 107
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[2] => Array (
[indices] => Array (
[0] => 108
[1] => 115
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[3] => Array (
[indices] => Array (
[0] => 116
[1] => 132
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array ( )
)
[twitter_lang] => de
[id] => tag:search.twitter.com,2005:554936456477933569
[retweetCount] => 1
[gnip] => Array (
[urls] => Array (
[0] => Array (
[expanded_url] => http://linkis.com/paper.li/visualsucce/7zYNk
[expanded_status] => 200
[url] => http://www.twitter.com
)
)
[language] => Array (
[value] => de
)
)
[object] => Array (
[postedTime] => 2015-01-13T08:04:48.000Z
[verb] => post
[link] => http://twitter.com/VisualSuccess/statuses/554911928527888384
[generator] => Array (
[displayName] => Linkis.com
[link] => http://linkis.com
)
[body] => "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com ://www.twitter.com
[objectType] => activity
[actor] => Array (
[summary] => Ù† (N), Wirtschaftsinformatiker | SAP Consultant Logistics #bigdata #appdevelopment #webdesign #eCommerce #SocialMedia #contentmarketing #SmartHome #Journal
[image] => https://pbs.twimg.com/profile_images/2841607223/959b0d23646b1f24bd7b70deac160e2f_normal.jpeg
[statusesCount] => 14185
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => VisualSuccess
[displayName] => Predrag Gasic
[postedTime] => 2010-11-08T17:19:27.000Z
[link] => http://www.twitter.com/VisualSuccess
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => low
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 58
[1] => 80
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 82
[1] => 88
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[1] => Array (
[indices] => Array (
[0] => 89
[1] => 96
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[2] => Array (
[indices] => Array (
[0] => 97
[1] => 113
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array (
)
)
)
)
)
Answers to this question would be greatly appreciated
Iterating $insightList will not get you to the cde level. And you don't actually want the cde; you want the message.
Just iterate the tweets. You don't need a foreach to get keyed info from a PHP array.
foreach($insightList['tweets'] as $tweet) {
$insight = $tweet['message']['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
It looks to me from your print_r that what you want is at:
echo $insightList[0]['message']['body'];
When there's a section like this [some_key], then 'some_key' is an associative array key, and you can access those elements be they additional arrays or a value, using the key name, as I illustrated here.
Related
I am trying to get the value of bank_name in php. Can't get it to display.
stdClass Object ( [account_id] => 43726384 [name] => Account Name [state] => action_required [description] => [owner_user_id] =>[type] => personal [create_time] => 1477684534 [disablement_time] => [country] => US [currencies] => Array ( [0] => USD ) [action_reasons] => Array ( [0] => kyc [1] => bank_account ) [disabled_reasons] => Array ( ) [image_uri] => [supported_card_types] => Array ( [0] => visa [1] => mastercard [2] => american_express [3] => discover [4] => jcb [5] => diners_club ) [gaq_domains] => Array ( [0] => ) [balances] => Array ( [0] => stdClass Object ( [balance] => 0 [currency] => USD [disputed_amount] => 0 [incoming_pending_amount] => 0 [outgoing_pending_amount] => 0 [reserved_amount] => 0 [bank_name] => Bank Of America [withdrawal_next_time] => [withdrawal_period] => [withdrawal_type] => ) ) [statuses] => Array ( [0] => stdClass Object ( [currency] => USD [incoming_payments_status] => ok [outgoing_payments_status] => paused [account_review_status] => not_requested ) ) )
It's not very clear if you display data that way, but I think this would be the way to access to bank_name:
$bankName = $object->balances[0]->bank_name;
(If it doesn't work, please show the XML before parsing it to an object)
$data = file_get_contents("https://graph.facebook.com/246179452202174/posts?access_token=TOKEN");
$data = json_decode($data, true);
print_r(array_values($data));
That's my code. It's simple. I can see the data and when I visit the link, it gives me the same info. So that works perfect. Underneath there's some outputs from the api.
Array ( [0] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) )
When I try to echo $data or maybe $data[1] or something like it, it wont work. Also removing array_values gives me a output of "Array" or "ArrayArray".
So how may I print out the data from the api? It wont work, also; how may I fetch the image? I'd like to print the title of the post, and the image. As a link for a feed at my site.
More response code for link:
Array ( [data] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) ) [1] => Array ( [id] => 246179452202174_680011585485623 [from] => Array ( [name] => Yoyo xno [category] => Musician/Band [id] => 281466768681242 ) [to] => Array ( [data] => Array ( [0] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) ) ) [message] => GG - Spotify: https://open.spotify.com/track/7tT6XVWfjtpNF8enpFpZT4 [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => wall_post [created_time] => 2016-05-12T17:33:49+0000 [updated_time] => 2016-05-12T17:33:49+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) ) [paging] => Array ( [cursors] => Array ( [before] => MTk2NDU4NjkwNzE2MzI5 [after] => MTk2NDU4NjkwNzE2MzI5 ) ) ) ) [2] => Array ( [id] => 246179452202174_676411742512274 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => Breaking news! Changes in the team. We are sad to announce that Aurora will part ways with Lgb Female. We wish to thank her for the time she spent with us and we wish her all the best for the future. Here's what she says about this herself: "My time with LGB eSports has come to an end. I am extremely thankful for the opportunity to grow within this team, go to amazing places and meet amazing people. I have learnt so much from this experience and I will never forget all the adventures we've had together! I would like to use this moment of change to look back and sincerely thank all my team-mates for this learning experience, Per Lilliefelth for looking after us, our sponsor Intel for taking care of us and LGB eSports for their immense contribution to helping us grow as an all-female team in Counter-Strike. I am certain I will still remain close to the team and look forward to seeing them at many future events. As for myself I am currently a free agent and look forward to continuing my journey as a competitive CS:GO player for many more years to come. Regards, Aurora Lyngdal" [picture] => https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13174168_676411742512274_4595019159434717947_n.jpg?oh=dad6bffaf94a2eae60c578d58f7cf8fc&oe=57E7E3F3 [link] => https://www.facebook.com/LGBeSports/photos/a.252900201530099.1073741829.246179452202174/676411742512274/?type=3 [name] => Timeline Photos [icon] => https://www.facebook.com/images/icons/photo.gif [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => photo [status_type] => added_photos [object_id] => 676411742512274 [created_time] => 2016-05-04T15:08:52+0000 [updated_time] => 2016-05-11T20:30:20+0000 [shares] => Array ( [count] => 4 ) [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1756499731303402 [name] => Antony Giordans ) [1] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) [2] => Array ( [id] => 10205058377778051 [name] => Charmaine Anne D. Better ) [3] => Array ( [id] => 992479757539742 [name] => Yunus Emre Çeker ) [4] => Array ( [id] => 244636029237859 [name] => Roman Hardinger ) [5] => Array ( [id] => 1134092109982746 [name] => Khoa Nguyen ) [6] => Array ( [id] => 525969817592366 [name] => Joseph Turton ) [7] => Array ( [id] => 1152533898100318 [name] => Kristian Sørensen ) [8] => Array ( [id] => 1193252910715325 [name] => Kamil Grochu Groszek ) [9] => Array ( [id] => 1158759100821275 [name] => Ace Kjirkovski ) [10] => Array ( [id] => 10153477966360064 [name]
you must echo this variable
$data[0][0]['message']
here is my youtube json response
query results:Array
(
[kind] => youtube#searchListResponse
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_RyGfO1dwDRqY9aMf412I8xvZgw"
[nextPageToken] => CAoQAA
[pageInfo] => Array
(
[totalResults] => 1000000
[resultsPerPage] => 10
)
[items] => Array
(
[0] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/vJBGevzCpIbHOcHu7AAc4WsQ92E"
[id] => Array
(
[kind] => youtube#video
[videoId] => eHfMfHyiWYA
)
[snippet] => Array
(
[publishedAt] => 2013-11-04T16:09:20.000Z
[channelId] => UCSvOdBJgMnTYsK-cZIGZSYQ
[title] => Ann Arbor - a test bed for driverless cars?
[description] => University of Michigan researchers work to make Ann Arbor the first American city with a shared fleet of connected and driverless vehicles by 2021. The cars of ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/hqdefault.jpg
)
)
[channelTitle] => michiganengineering
[liveBroadcastContent] => none
)
)
[1] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9Mm7w3esGwaHCTasG_67T33aULE"
[id] => Array
(
[kind] => youtube#video
[videoId] => PRvGaCnfV-8
)
[snippet] => Array
(
[publishedAt] => 2009-01-26T16:50:35.000Z
[channelId] => UCPkN7EcOJlQAzCLxaZSn_5g
[title] => Timelapse Cars
[description] =>
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/hqdefault.jpg
)
)
[channelTitle] => frehleycomet
[liveBroadcastContent] => none
)
)
[2] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9_WnlpVsvx2WLKqdgxGXI3CRvBE"
[id] => Array
(
[kind] => youtube#video
[videoId] => U60TfgpHqSc
)
[snippet] => Array
(
[publishedAt] => 2013-07-01T23:23:59.000Z
[channelId] => UCULJH9kW-UdTBCDu27P0BoA
[title] => Are cars killing the Bicycle Kingdom?
[description] => Bike advocacy in the Bicycle Kingdom? China was once known as the Bicycle Kingdom but with the growth of the automobile, the nature of biking in Beijing is ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/hqdefault.jpg
)
)
[channelTitle] => llukitsch
[liveBroadcastContent] => none
)
)
[3] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_2N5Lkva8p_pbJqLf5K61gNPHL0"
[id] => Array
(
[kind] => youtube#video
[videoId] => Xg5-oxTSM04
)
[snippet] => Array
(
[publishedAt] => 2014-02-13T17:42:31.000Z
[channelId] => UC-Gu_VkJmS1FGxsrSddKvfg
[title] => Market America's 2014 World Conference: FreeBee Cars
[description] => Market America's 2014 World Conference in Miami featured FreeBee cars showcasing MA brands like Motives® Cosmetics and DNA Miracles™.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/hqdefault.jpg
)
)
[channelTitle] => MarketAmerica
[liveBroadcastContent] => none
)
)
[4] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/VXZohXlA5kHPvrJ-rxpdEjUsaFY"
[id] => Array
(
[kind] => youtube#video
[videoId] => yrcq5awviik
)
[snippet] => Array
(
[publishedAt] => 2011-06-01T23:31:01.000Z
[channelId] => UC1URrXTrZ68pZZ4YDmT4WVQ
[title] => Ford's connected cars get street-smart
[description] => At AT&T Park in San Francisco, SmartPlanet correspondent Sumi Das talks to Ford's Mike Shulman about the company's research into intelligent vehicles.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/hqdefault.jpg
)
)
[channelTitle] => SmartPlanetCBS
[liveBroadcastContent] => none
)
)
[5] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/FWmwcjir_8QSK6eq5D5gsY-lWXQ"
[id] => Array
(
[kind] => youtube#video
[videoId] => bFV7WU6UHaQ
)
[snippet] => Array
(
[publishedAt] => 2014-05-14T10:59:48.000Z
[channelId] => UCI1H2wxkvshpCcUNNOEc4Gw
[title] => Google's Driverless Cars Take to the Streets
[description] => Google's self-driving car technology is not likely to be available for several more years. But the company is already beginning the job of making the public ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/hqdefault.jpg
)
)
[channelTitle] => IBTimesUK
[liveBroadcastContent] => none
)
)
[6] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/t7dSXvQ-37jK0DFYHxk4iP1o3-s"
[id] => Array
(
[kind] => youtube#video
[videoId] => ifzSVGEHxMU
)
[snippet] => Array
(
[publishedAt] => 2012-10-22T13:45:39.000Z
[channelId] => UC6s5vC0tCuStbt4d5dfhmjA
[title] => Project C.A.R.S: BMW Z4
[description] => Scott tries out the newest car to be added, the BMW Z4. Those of you waiting for Tekkit, it will return soon! Hound is currently in the process of moving! Pl...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/hqdefault.jpg
)
)
[channelTitle] => scott2k44
[liveBroadcastContent] => none
)
)
[7] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/RW6QW1hjACRmSo6Za1BzsP1gbSk"
[id] => Array
(
[kind] => youtube#video
[videoId] => YZggnfolreA
)
[snippet] => Array
(
[publishedAt] => 2015-05-08T21:32:35.000Z
[channelId] => UC87geOOB2rCWkJFtUUeEBsw
[title] => Project CARS vs. DriveClub | Graphics, Sound, Rain & Weather Effects Gameplay Comparison (PS4)
[description] => Comparing the biggest racing games available on the PS4, DriveClub (PS4 exclusive) and Project CARS (also available on PC and Xbox One). Both games are ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/hqdefault.jpg
)
)
[channelTitle] => VideoGamesNL
[liveBroadcastContent] => none
)
)
[8] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/OIAM0bSsr9oHqPwPPg3msZUqeMU"
[id] => Array
(
[kind] => youtube#video
[videoId] => hJxFid8A0Xw
)
[snippet] => Array
(
[publishedAt] => 2015-04-02T16:00:05.000Z
[channelId] => UCNjakstwb629k6IE9BualRw
[title] => Comedians In Cars Getting Coffee: Single Shot - Analogy Lad
[description] => Watch it on Crackle: [http://bit.ly/SingleShot] Jerry's special guests are Todd Barry, George Wallace, Sarah Silverman, Fred Armisen, Kevin Hart and Jimmy ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/hqdefault.jpg
)
)
[channelTitle] => Crackle
[liveBroadcastContent] => none
)
)
[9] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/Q945pOROs0oCGIOcFFq66OWZZdw"
[id] => Array
(
[kind] => youtube#video
[videoId] => I00AXDv8ncY
)
[snippet] => Array
(
[publishedAt] => 2015-05-07T07:03:27.000Z
[channelId] => UCyQCcFWSh7MLAhdq2a_2jKA
[title] => Project CARS Online #1 - Los gehts! - Formula A #Monza | Wheel/Controller
[description] => Project CARS ist ein Motorsport Multiplayer-Simulation Spiel, entwickelt und veröffentlicht vom britischen Video-Spiel-Entwickler Slightly Mad Studios.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/hqdefault.jpg
)
)
[channelTitle] =>
[liveBroadcastContent] => none
)
)
)
)
i want to extract video title, thumbnail, description from this json response using foreach loop
i tried this one but its gives me an error please help me to solve this issues.
my php code is
$link = 'https://www.googleapis.com/youtube/v3/search?order=rating&part=snippet&q=cars&maxResults=10&key={KEY}';
$video = file_get_contents($link);
$video = json_decode($video, true);
echo '<pre>';
print_r("query results:");
print_r($video);
'</pre>';
foreach ($video->items as $data ){
echo "{$data->snippet->title}";
echo "<br>";
}
$video is an array, not an object, use $video['items'], the same with $data etc.
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
I need to get the data for each language in it's field as an array.
Probably the best approach in CakePHP will be Set::combine but can't get it working.
I can do it manually with foreach but I don't think that will be the best way.
Here is the example:
Array
(
[Article] => Array
(
[id] => 131
[title] => TEST
)
[titleTranslation] => Array
(
[0] => Array
(
[id] => 62
[locale] => eng
[model] => Article
[foreign_key] => 131
[field] => title
[content] => TEST
)
[1] => Array
(
[id] => 63
[locale] => fre
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Salva
)
[2] => Array
(
[id] => 64
[locale] => rus
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Пвет
)
)
)
into this array:
Array
(
[Article] => Array
(
[id] => 131
[title] => Array
(
[eng] => TEST
[fre] => Salva
[rus] => Пвет
)
)
.... the rest is not important
)
Solved-----
$translatedData = Set::combine($this->data['titleTranslation'], '{n}.locale', '{n}.content', '{n}.field');
$this->data['Article'] = array_merge($this->data['Article'], $translatedData);