Adding an element to a json object from twitter response - php

Im trying to ad an attribute to an arraay but i keep coming up with these errors:
Warning: json_decode() expects parameter 1 to be string, object given in C:\xampp\htdocs\4th\twitdb2\get_tweets.php on line 25
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\4th\twitdb2\get_tweets.php on line 27
null
I want to ad is_selected to the end of each element of the array but i cant get my head around it. any help would be much appreciate it. thank you
<?php
session_start();
require_once("twitteroauth.php"); //Path to twitteroauth library
$search = "xxxxx";
$geocode = "xxxxx";
$notweets = xxxxxx;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxxxx";
$accesstoken = "xxxxx-xxxxxx";
$accesstokensecret = "xxxxxxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$search = str_replace("#", "%23", $search);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);
$result = json_decode($tweets, true);
foreach($result as $key => $val)
{
$result[$key]['is_selected'] = 0;
}
echo json_encode($result);
?>
var dump
object(stdClass)#5 (2) { ["statuses"]=> array(13) { [0]=> object(stdClass)#6 (23) { ["metadata"]=> object(stdClass)#7 (2) { ["result_type"]=> string(6) "recent" ["iso_language_code"]=> string(2) "nl" } ["created_at"]=> string(30) "Wed Mar 05 11:11:46 +0000 2014" ["id"]=> float(4.411691911448E+17) ["id_str"]=> string(18) "441169191144796160" ["text"]=> string(29) "iPhone charger is broken 😤" ["source"]=> string(82) "Twitter for iPhone" ["truncated"]=> bool(false) ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> object(stdClass)#8 (40) { ["id"]=> int(31950251) ["id_str"]=> string(8) "31950251" ["name"]=> string(17) "Rebecca McDermott" ["screen_name"]=> string(10) "rebeccamcd" ["location"]=> string(0) "" ["description"]=> string(0) "" ["url"]=> NULL ["entities"]=> object(stdClass)#9 (1) { ["description"]=> object(stdClass)#10 (1) { ["urls"]=> array(0) { } } } ["protected"]=> bool(false) ["followers_count"]=> int(53) ["friends_count"]=> int(143) ["listed_count"]=> int(1) ["created_at"]=> string(30) "Thu Apr 16 16:26:14 +0000 2009" ["favourites_count"]=> int(516) ["utc_offset"]=> int(0) ["time_zone"]=> string(6) "Dublin" ["geo_enabled"]=> bool(true) ["verified"]=> bool(false) ["statuses_count"]=> int(1312) ["lang"]=> string(2) "en" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["is_translation_enabled"]=> bool(false) ["profile_background_color"]=> string(6) "ACDED6" ["profile_background_image_url"]=> string(49) "http://abs.twimg.com/images/themes/theme18/bg.gif" ["profile_background_image_url_https"]=> string(50) "https://abs.twimg.com/images/themes/theme18/bg.gif" ["profile_background_tile"]=> bool(false) ["profile_image_url"]=> string(75) "http://pbs.twimg.com/profile_images/432285869324922880/QQiT0W7B_normal.jpeg" ["profile_image_url_https"]=> string(76) "https://pbs.twimg.com/profile_images/432285869324922880/QQiT0W7B_normal.jpeg" ["profile_banner_url"]=> string(57) "https://pbs.twimg.com/profile_banners/31950251/1379612844" ["profile_link_color"]=> string(6) "038543" ["profile_sidebar_border_color"]=> string(6) "EEEEEE" ["profile_sidebar_fill_color"]=> string(6) "F6F6F6" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> bool(false) ["follow_request_sent"]=> bool(false) ["notifications"]=> bool(false) } ["geo"]=> object(stdClass)#11 (2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(54.98074354) [1]=> float(-7.30069342) } } ["coordinates"]=> object(stdClass)#12 (2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-7.30069342) [1]=> float(54.98074354) } } ["place"]=> object(stdClass)#13 (10) { ["id"]=> string(16) "f2ea0703dc6c7579" ["url"]=> string(56) "https://api.twitter.com/1.1/geo/id/f2ea0703dc6c7579.json" ["place_type"]=> string(4) "city" ["name"]=> string(5) "Derry" ["full_name"]=> string(12) "Derry, Derry" ["country_code"]=> string(2) "GB" ["country"]=> string(14) "United Kingdom" ["contained_within"]=> array(0) { } ["bounding_box"]=> object(stdClass)#14 (2) { ["type"]=> string(7) "Polygon" ["coordinates"]=> array(1) { [0]=> array(4) { [0]=> array(2) { [0]=> float(-7.4070791) [1]=> float(54.8110645) } [1]=> array(2) { [0]=> float(-7.0069043) [1]=> float(54.8110645) } [2]=> array(2) { [0]=> float(-7.0069043) [1]=> float(55.0668188) } [3]=> array(2) { [0]=> float(-7.4070791) [1]=> float(55.0668188) } } } } ["attributes"]=> object(stdClass)#15 (0) { } } ["contributors"]=> NULL ["retweet_count"]=> int(0) ["favorite_count"]=> int(1) ["entities"]=> object(stdClass)#16 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["urls"]=> array(0) { } ["user_mentions"]=> array(0) { } }

This should do the trick:
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);
for($i = 0; $i < count($tweets->statuses); $i++) {
$tweets->statuses[$i]['is_selected'] = 0;
}
echo json_encode($result);

Related

How to access array value and place into string

i have put a returned API object response into an array but now i want to access the array data, i get undefined index, this is my code.
$paystack = new Yabacon\Paystack('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$trx = $paystack->transactions(["perPage"=>1, "amount"=>10000, "status"=>"success"]); //RETURNED OBJECT
$array = (array) $trx; //CONVERTED FROM OBJECT TO ARRAY.
When I var_dump( (array) $trx ); i get
array(4) { ["status"]=> bool(true) ["message"]=> string(22) "Transactions retrieved" ["data"]=> array(1) { [0]=> object(stdClass)#21 (22) { ["id"]=> int(901742) ["domain"]=> string(4) "test" ["status"]=> string(7) "success" ["reference"]=> string(13) "58c0e61ca5cce" ["amount"]=> int(10000) ["message"]=> NULL ["gateway_response"]=> string(10) "Successful" ["paid_at"]=> string(24) "2017-03-08T22:48:33.000Z" ["created_at"]=> string(24) "2017-03-08T22:48:11.000Z" ["channel"]=> string(4) "card" ["currency"]=> string(3) "NGN" ["ip_address"]=> string(14) "xxx.xxx.xxx.xxx" ["metadata"]=> object(stdClass)#23 (2) { ["custom_fields"]=> array(1) { [0]=> object(stdClass)#22 (3) { ["display_name"]=> string(13) "Mobile Number" ["variable_name"]=> string(13) "mobile_number" ["value"]=> string(14) "+xxxxxxxxxxxxx" } } ["referrer"]=> string(27) "http://localhost/b/checkout" } ["log"]=> object(stdClass)#24 (9) { ["time_spent"]=> int(24) ["attempts"]=> int(1) ["authentication"]=> NULL ["errors"]=> int(0) ["success"]=> bool(true) ["mobile"]=> bool(false) ["input"]=> array(0) { } ["channel"]=> NULL ["history"]=> array(4) { [0]=> object(stdClass)#25 (3) { ["type"]=> string(5) "input" ["message"]=> string(55) "Filled these fields: card number, card expiry, card cvv" ["time"]=> int(20) } [1]=> object(stdClass)#26 (3) { ["type"]=> string(6) "action" ["message"]=> string(16) "Attempted to pay" ["time"]=> int(20) } [2]=> object(stdClass)#27 (3) { ["type"]=> string(7) "success" ["message"]=> string(17) "Successfully paid" ["time"]=> int(22) } [3]=> object(stdClass)#28 (3) { ["type"]=> string(5) "close" ["message"]=> string(11) "Page closed" ["time"]=> int(24) } } } ["fees"]=> int(150) ["fees_split"]=> NULL ["customer"]=> object(stdClass)#29 (8) { ["id"]=> int(102185) ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["email"]=> string(23) "xxxxxxxxxx#gmail.com" ["customer_code"]=> string(19) "CUS_xxxxxxxxxxx" ["phone"]=> string(0) "" ["metadata"]=> NULL ["risk_action"]=> string(7) "default" } ["authorization"]=> object(stdClass)#30 (12) { ["authorization_code"]=> string(15) "AUTH_xxxxxxxxx" ["bin"]=> string(6) "412345" ["last4"]=> string(4) "1381" ["exp_month"]=> string(2) "01" ["exp_year"]=> string(4) "2020" ["channel"]=> string(4) "card" ["card_type"]=> string(4) "visa" ["bank"]=> string(9) "TEST BANK" ["country_code"]=> string(2) "NG" ["brand"]=> string(4) "visa" ["reusable"]=> bool(true) ["signature"]=> string(24) "SIG_xxxxxxxxxxxx" } ["plan"]=> object(stdClass)#31 (0) { } ["subaccount"]=> object(stdClass)#32 (0) { } ["paidAt"]=> string(24) "2017-03-08T22:48:33.000Z" ["createdAt"]=> string(24) "2017-03-08T22:48:11.000Z" } } ["meta"]=> object(stdClass)#33 (6) { ["total"]=> int(6) ["total_volume"]=> int(60000) ["skipped"]=> int(0) ["perPage"]=> string(1) "1" ["page"]=> int(1) ["pageCount"]=> int(6) } }
What I want to do is, I want to get it in this format
$stats = $array['data']['status'];
$order_id = $array['data']['id'];
$transaction_method = $array['data']['channel'];
$currency = $array['data']['currency'];
$reference = $array['data']['reference'];
$final_price = $array['data']['amount'];
$email_send = $array['data']['customer']['email'];
$day_paid = $array['data']['paid_at'];
$referring_page = $array['data']['metadata']['referrer'];
But when i ECHO any variable out e.g i do echo $stats OR echo $order_id i get
Notice: Undefined index: status in C:\xampp\htdocs\b\d\paystackorders.php on line 150
Notice: Undefined index: id in C:\xampp\htdocs\b\d\paystackorders.php on line 151.
How do i go around resolving this.
It needs to be $array['data'][0]['status']
and $array['data'][0]['id']
and so on...
This is because $array['data'] is an array of objects
The best way to go in my opinion is as follows:
$data = $array['data'][0];
And then access what you want :
$status = $data['status'];
$order_id = $data['id'];

Sorting arrays and arrays of arrays using a key parameter

I have array of students which is fetched in a variable $arraytosort and looks like
array(5027) { [0]=> array(5) { ["_id"]=> object(MongoId)#22 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["fullname"]=> string(19) "David Robert Lara" ["firstmiddle"]=> string(12) "David Robert" ["firstlast"]=> string(14) "David Lara" ["student"]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "David" ["middle_name"]=> string(4) "Robert" ["last_name"]=> string(6) "Lara" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } }
...
...
}
I have written below function to sort these students on the basis of key value given in $key so that the array will contain the similar results on top/first.
$sortedarray = array();
$sortedarray = $this->multiSort(
$arraytosort,
$key,
'fullname',
'firstmiddle',
'firstlast',
'first_name',
'middle_name',
'last_name',
'roll_no',
'email'
);
function multiSort()
{
$args = func_get_args();
$c = count($args);
if ($c < 2) {
return false;
}
$key= $args[1];
$array = array_splice($args, 0, 1);
$array = $array[0];
usort($array, function($a) use($args) {
$i = 0;
$c = count($args);
$cmp = 0;
while($cmp == 0 && $i < $c)
{
$cmp = stripos($a[$args[$i] ],$key);
$i++;
}
return $cmp;
});
return $array;
}
It throws error message "Undefined variable: key".
Please help!!!

How can I extract values from the array of JSON data that this var_dump gives [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
How can I extract title and content from the array of JSON data that gives the following when I var_dump(WpApi::posts());
array(3) { ["results"]=> array(1)
{ [0]=> array(23)
{ ["id"]=> int(8)
["date"]=> string(19) "2017-01-31T07:08:21"
["date_gmt"]=> string(19) "2017-01-31T07:08:21"
["guid"]=> array(1) { ["rendered"]=> string(34) "http://idybrand.com/wordpress/?p=8" }
["modified"]=> string(19) "2017-01-31T07:08:21"
["modified_gmt"]=> string(19) "2017-01-31T07:08:21"
["slug"]=> string(34) "february-is-just-around-the-corner"
["type"]=> string(4) "post"
["link"]=> string(76) "http://idybrand.com/wordpress/2017/01/31/february-is-just-around-the-corner/"
["title"]=> array(1) { ["rendered"]=> string(34) "February is just around the corner" }
["content"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) }
["excerpt"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) }
["author"]=> int(1)
["featured_media"]=> int(0)
["comment_status"]=> string(4) "open"
["ping_status"]=> string(4) "open"
["sticky"]=> bool(false)
["template"]=> string(0) ""
["format"]=> string(8) "standard"
["meta"]=> array(0) { }
["categories"]=> array(1) { [0]=> int(1) }
["tags"]=> array(0) { }
["_links"]=> array(9) { ["self"]=> array(1) { [0]=> array(1) { ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8" } } ["collection"]=> array(1) { [0]=> array(1) { ["href"]=> string(49) "http://idybrand.com/wordpress/wp-json/wp/v2/posts" } } ["about"]=> array(1) { [0]=> array(1) { ["href"]=> string(54) "http://idybrand.com/wordpress/wp-json/wp/v2/types/post" } } ["author"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/users/1" } } ["replies"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(59) "http://idybrand.com/wordpress/wp-json/wp/v2/comments?post=8" } } ["version-history"]=> array(1) { [0]=> array(1) { ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8/revisions" } } ["wp:attachment"]=> array(1) { [0]=> array(1) { ["href"]=> string(58) "http://idybrand.com/wordpress/wp-json/wp/v2/media?parent=8" } } ["wp:term"]=> array(2) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["embeddable"]=> bool(true) ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/categories?post=8" } [1]=> array(3) { ["taxonomy"]=> string(8) "post_tag" ["embeddable"]=> bool(true) ["href"]=> string(55) "http://idybrand.com/wordpress/wp-json/wp/v2/tags?post=8" } } ["curies"]=> array(1) { [0]=> array(3) { ["name"]=> string(2) "wp" ["href"]=> string(23) "https://api.w.org/{rel}" ["templated"]=> bool(true) } } } } }
["total"]=> string(1) "1"
["pages"]=> string(1) "1" }
Using PHP please
This will do it according to your JSON.
echo ['results'][0]['title']['rendered']; //Title
echo ['results'][0]['content']['rendered']; //Content
You've got a multi-dimensional array so you've to get data from it.
$arr = WpApi::posts();
$arr['results'][0]['content']['rendered'] //this is your content string
$arr['results'][0]['title']['rendered'] // this is your title string

Php Json get first element info

so I'm using a PHP Twitter API to grab a list of tweets with a certain hashtag. I'm looking to get the following info from the returned json; 'screen_name' and 'text'.
The screen_name is on line 44 and the text is on line 20.
Here is the json output.
object(stdClass)#5 (2) {
["statuses"]=>
array(15) {
[0]=>
object(stdClass)#6 (24) {
["metadata"]=>
object(stdClass)#7 (2) {
["iso_language_code"]=>
string(2) "en"
["result_type"]=>
string(6) "recent"
}
["created_at"]=>
string(30) "Fri Dec 05 21:22:00 +0000 2014"
["id"]=>
float(5.4097942452372E+17)
["id_str"]=>
string(18) "540979424523718656"
["text"]=>
string(100) "Shits getting real. Crit happens tonight. #dnd #dungeonsanddragons #nerd #rpg http://t.co/44Lnrmmfte"
["source"]=>
string(59) "Instagram"
["truncated"]=>
bool(false)
["in_reply_to_status_id"]=>
NULL
["in_reply_to_status_id_str"]=>
NULL
["in_reply_to_user_id"]=>
NULL
["in_reply_to_user_id_str"]=>
NULL
["in_reply_to_screen_name"]=>
NULL
["user"]=>
object(stdClass)#8 (41) {
["id"]=>
int(1297196190)
["id_str"]=>
string(10) "1297196190"
["name"]=>
string(12) "Brandon Wade"
["screen_name"]=>
string(15) "Brandonus_Prime"
["location"]=>
string(15) "The woods of NC"
["profile_location"]=>
NULL
["description"]=>
string(29) "If it bleeds, we can kill it."
["url"]=>
string(22) "http://t.co/EkGUdm9yDO"
["entities"]=>
object(stdClass)#9 (2) {
["url"]=>
object(stdClass)#10 (1) {
["urls"]=>
array(1) {
[0]=>
object(stdClass)#11 (4) {
["url"]=>
string(22) "http://t.co/EkGUdm9yDO"
["expanded_url"]=>
string(34) "http://facebook.com/brandonusprime"
["display_url"]=>
string(27) "facebook.com/brandonusprime"
["indices"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(22)
}
}
}
}
["description"]=>
object(stdClass)#12 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(44)
["friends_count"]=>
int(246)
["listed_count"]=>
int(0)
["created_at"]=>
string(30) "Mon Mar 25 01:10:47 +0000 2013"
["favourites_count"]=>
int(18)
["utc_offset"]=>
NULL
["time_zone"]=>
NULL
["geo_enabled"]=>
bool(false)
["verified"]=>
bool(false)
["statuses_count"]=>
int(170)
["lang"]=>
string(2) "en"
["contributors_enabled"]=>
bool(false)
["is_translator"]=>
bool(false)
["is_translation_enabled"]=>
bool(false)
["profile_background_color"]=>
string(6) "C0DEED"
["profile_background_image_url"]=>
string(48) "http://abs.twimg.com/images/themes/theme1/bg.png"
["profile_background_image_url_https"]=>
string(49) "https://abs.twimg.com/images/themes/theme1/bg.png"
["profile_background_tile"]=>
bool(false)
["profile_image_url"]=>
string(99) "http://pbs.twimg.com/profile_images/378800000698263057/6592d36cf8e2beb02c9f3ee9abf4fdd7_normal.jpeg"
["profile_image_url_https"]=>
string(100) "https://pbs.twimg.com/profile_images/378800000698263057/6592d36cf8e2beb02c9f3ee9abf4fdd7_normal.jpeg"
["profile_banner_url"]=>
string(59) "https://pbs.twimg.com/profile_banners/1297196190/1364236862"
["profile_link_color"]=>
string(6) "0084B4"
["profile_sidebar_border_color"]=>
string(6) "C0DEED"
["profile_sidebar_fill_color"]=>
string(6) "DDEEF6"
["profile_text_color"]=>
string(6) "333333"
["profile_use_background_image"]=>
bool(true)
["default_profile"]=>
bool(true)
["default_profile_image"]=>
bool(false)
["following"]=>
bool(false)
["follow_request_sent"]=>
bool(false)
["notifications"]=>
bool(false)
}
["geo"]=>
NULL
["coordinates"]=>
NULL
["place"]=>
NULL
["contributors"]=>
NULL
["retweet_count"]=>
int(0)
["favorite_count"]=>
int(0)
["entities"]=>
object(stdClass)#13 (4) {
["hashtags"]=>
array(4) {
[0]=>
object(stdClass)#14 (2) {
["text"]=>
string(3) "dnd"
["indices"]=>
array(2) {
[0]=>
int(42)
[1]=>
int(46)
}
}
[1]=>
object(stdClass)#15 (2) {
["text"]=>
string(18) "dungeonsanddragons"
["indices"]=>
array(2) {
[0]=>
int(47)
[1]=>
int(66)
}
}
[2]=>
object(stdClass)#16 (2) {
["text"]=>
string(4) "nerd"
["indices"]=>
array(2) {
[0]=>
int(67)
[1]=>
int(72)
}
}
[3]=>
object(stdClass)#17 (2) {
["text"]=>
string(3) "rpg"
["indices"]=>
array(2) {
[0]=>
int(73)
[1]=>
int(77)
}
}
}
["symbols"]=>
array(0) {
}
["user_mentions"]=>
array(0) {
}
["urls"]=>
array(1) {
[0]=>
object(stdClass)#18 (4) {
["url"]=>
string(22) "http://t.co/44Lnrmmfte"
["expanded_url"]=>
string(34) "http://instagram.com/p/wPV3gmr1kl/"
["display_url"]=>
string(27) "instagram.com/p/wPV3gmr1kl/"
["indices"]=>
array(2) {
[0]=>
int(78)
[1]=>
int(100)
}
}
}
}
["favorited"]=>
bool(false)
["retweeted"]=>
bool(false)
["possibly_sensitive"]=>
bool(false)
["lang"]=>
string(2) "en"
}
}
Here is my PHP Code
<?php
require_once 'twitteroauth/twitteroauth.php';
define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('ACCESS_TOKEN', '-');
define('ACCESS_TOKEN_SECRET', '');
$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$query = array(
"q" => "#nerd",
"result_type" => "recent"
);
$results = $toa->get('search/tweets', $query);
var_dump($results); die();
foreach ($results->statuses as $result) {
echo $result->user->screen_name . ": " . $result->text . "<br>";
}
Thanks a ton! I've been trying for a while and need some pointing in the right direction :)

PHP std::Class check if attribute exists

I am using Twitter Search API and it returs an array of objects. I have to check for each object if it contains ['media'] array. Below is one of the objects with media attribute.
object(stdClass)#56 (4) {
["hashtags"]=>
array(2) {
[0]=>
object(stdClass)#57 (2) {
["text"]=>
string(9) "katyperry"
["indices"]=>
array(2) {
[0]=>
int(62)
[1]=>
int(72)
}
}
[1]=>
object(stdClass)#58 (2) {
["text"]=>
string(8) "katycats"
["indices"]=>
array(2) {
[0]=>
int(73)
[1]=>
int(82)
}
}
}
["urls"]=>
array(0) {
}
["user_mentions"]=>
array(1) {
[0]=>
object(stdClass)#59 (5) {
["screen_name"]=>
string(13) "perfkatyperry"
["name"]=>
string(7) "KatyCat"
["id"]=>
int(611836599)
["id_str"]=>
string(9) "611836599"
["indices"]=>
array(2) {
[0]=>
int(3)
[1]=>
int(17)
}
}
}
["media"]=>
array(1) {
[0]=>
object(stdClass)#60 (10) {
["id"]=>
int(325747221490434048)
["id_str"]=>
string(18) "325747221490434048"
["indices"]=>
array(2) {
[0]=>
int(83)
[1]=>
int(105)
}
["media_url"]=>
string(46) "http://pbs.twimg.com/media/BIVJXz-CAAAHQ8M.jpg"
["media_url_https"]=>
string(47) "https://pbs.twimg.com/media/BIVJXz-CAAAHQ8M.jpg"
["url"]=>
string(22) "http://t.co/RYgQ8Srze5"
["display_url"]=>
string(26) "pic.twitter.com/RYgQ8Srze5"
["expanded_url"]=>
string(66) "http://twitter.com/perfkatyperry/status/325747221486239744/photo/1"
["type"]=>
string(5) "photo"
["sizes"]=>
object(stdClass)#61 (5) {
["medium"]=>
object(stdClass)#62 (3) {
["w"]=>
int(600)
["h"]=>
int(600)
["resize"]=>
string(3) "fit"
}
["thumb"]=>
object(stdClass)#63 (3) {
["w"]=>
int(150)
["h"]=>
int(150)
["resize"]=>
string(4) "crop"
}
["large"]=>
object(stdClass)#64 (3) {
["w"]=>
int(1024)
["h"]=>
int(1024)
["resize"]=>
string(3) "fit"
}
["small"]=>
object(stdClass)#65 (3) {
["w"]=>
int(340)
["h"]=>
int(340)
["resize"]=>
string(3) "fit"
}
["orig"]=>
object(stdClass)#66 (3) {
["w"]=>
int(1024)
["h"]=>
int(1024)
["resize"]=>
string(3) "fit"
}
}
}
}
}
How can i do that?
Thanks!
Use a loop + property_exists function:
foreach($myArray as $obj)
{
if(!property_exists($obj, 'media') || !is_array($obj->media))
{
echo("property 'media' does not exist in $obj");
// throw exception, fill in default value for media array, set variable, whatever.
}
}

Categories