Php Json get first element info - php

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 :)

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'];

JSON parsing in php to display the server response sabre

After making the authentication in sabre from php and make an inquiry about a flight, the response is a JSON string that I can not decipher, someone aid me, as I can show this readable string in php for my project.
part of the code, thanks for your help
object(SharedContext)#3 (1) {
["results":"SharedContext":private]=>
array(7) {
["SECURITY"]=>``
NULL
["origin"]=>
string(3) "MTR"
["destination"]=>
string(3) "BOG"
["departureDate"]=>
string(10) "2016-06-10"
["LeadPriceCalendar"]=>
object(stdClass)#6 (5) {
["status"]=>
string(8) "Complete"
["type"]=>
string(11) "Application"
["errorCode"]=>
string(20) "WARN.RAF.APPLICATION"
["timeStamp"]=>
string(25) "2016-06-03T22:04:40+00:00"
["message"]=>
string(21) "No results were found"
}
["InstaFlight"]=>
NULL
["BargainFinderMax"]=>
object(stdClass)#4 (2) {
["OTA_AirLowFareSearchRS"]=>
object(stdClass)#10 (11) {
["PricedItinCount"]=>
int(6)
["BrandedOneWayItinCount"]=>
int(0)
["SimpleOneWayItinCount"]=>
int(0)
["DepartedItinCount"]=>
int(0)
["SoldOutItinCount"]=>
int(0)
["AvailableItinCount"]=>
int(0)
["Version"]=>
string(5) "1.8.6"
["Success"]=>
object(stdClass)#11 (0) {
}
["Warnings"]=>
object(stdClass)#12 (1) {
["Warning"]=>
array(4) {
[0]=>
object(stdClass)#13 (5) {
["Type"]=>
string(12) "WORKERTHREAD"
["ShortText"]=>
string(19) "1750772809123328107"
["Code"]=>
string(13) "TRANSACTIONID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[1]=>
object(stdClass)#14 (5) {
["Type"]=>
string(6) "SERVER"
["ShortText"]=>
string(5) "27037"
["Code"]=>
string(9) "TTFHLC702"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[2]=>
object(stdClass)#15 (5) {
["Type"]=>
string(7) "DEFAULT"
["ShortText"]=>
string(5) "13330"
["Code"]=>
string(6) "RULEID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[3]=>
object(stdClass)#16 (5) {
["Type"]=>
string(3) "DRE"
["ShortText"]=>
string(5) "13291"
["Code"]=>
string(6) "RULEID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
}
}
["PricedItineraries"]=>
object(stdClass)#17 (1) {
["PricedItinerary"]=>
array(6) {
[0]=>
object(stdClass)#18 (5) {
["SequenceNumber"]=>
int(1)
["AirItinerary"]=>
object(stdClass)#19 (2) {
["DirectionInd"]=>
string(6) "OneWay"
["OriginDestinationOptions"]=>
object(stdClass)#20 (1) {
["OriginDestinationOption"]=>
array(1) {
[0]=>
object(stdClass)#21 (2) {
["ElapsedTime"]=>
int(76)
["FlightSegment"]=>
array(1) {
[0]=>
object(stdClass)#22 (15) {
["DepartureDateTime"]=>
string(19) "2016-06-10T08:38:00"
["ArrivalDateTime"]=>
string(19) "2016-06-10T09:54:00"
["StopQuantity"]=>
int(0)
["FlightNumber"]=>
string(4) "3193"
["ResBookDesigCode"]=>
string(1) "Q"
["ElapsedTime"]=>
int(76)
["DepartureAirport"]=>
object(stdClass)#23 (2) {
["LocationCode"]=>
string(3) "MTR"
["content"]=>
string(0) ""
}
["ArrivalAirport"]=>
object(stdClass)#24 (3) {
["LocationCode"]=>
string(3) "BOG"
["TerminalID"]=>
string(1) "1"
["content"]=>
string(0) ""
}
["OperatingAirline"]=>
object(stdClass)#25 (4) {
["CompanyShortName"]=>
string(26) "LAN COLOMBIA AIRLINES S.A."
["Code"]=>
string(2) "LA"
["FlightNumber"]=>
string(4) "3193"
["content"]=>
string(0) ""
}
["Equipment"]=>
array(1) {
[0]=>
object(stdClass)#26 (2) {
["AirEquipType"]=>
string(3) "320"
["content"]=>
string(0) ""
}
}
["MarketingAirline"]=>
object(stdClass)#27 (2) {
["Code"]=>
string(2) "LA"
["content"]=>
string(0) ""
}
["MarriageGrp"]=>
string(1) "O"
["DepartureTimeZone"]=>
object(stdClass)#28 (1) {
["GMTOffset"]=>
float(-5)
}
["ArrivalTimeZone"]=>
object(stdClass)#29 (1) {
["GMTOffset"]=>
float(-5)
}
["TPA_Extensions"]=>
object(stdClass)#30 (1) {
["eTicket"]=>
object(stdClass)#31 (1) {
["Ind"]=>
bool(true)
}
}
}
}
}
}
}
}

Adding an element to a json object from twitter response

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);

Exclude Retweets in Twitter Search API 1.1

Here I'm trying to retrieve the tweets which has the tag 'V57' using search api 1.1. Dumped & seen the results of search term by using,
$search_tim = $connection->get('search/tweets',array('q' => ' #V57', 'count' => 5, 'result_type' => 'recent'));
This is the result after doing var_dump :
object(stdClass)#10 (2) {
["statuses"]=>
array(5) {
[0]=>
object(stdClass)#11 (25) {
["metadata"]=>
object(stdClass)#12 (2) {
["result_type"]=>
string(6) "recent"
["iso_language_code"]=>
string(2) "en"
}
["created_at"]=>
string(30) "Wed Feb 12 16:26:35 +0000 2014"
["id"]=>
float(4.33638273555E+17)
["id_str"]=>
string(18) "4336382731354561"
["text"]=>
string(131) "RT #useracc: Plz consult a doctor #V57"
["source"]=>
string(84) "Twitter for Android"
["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)#13 (40) {
["id"]=>
int(965892252)
["id_str"]=>
string(9) "965892252"
["name"]=>
string(10) "khan"
["screen_name"]=>
string(10) "khan"
["location"]=>
string(0) ""
["url"]=>
NULL
["entities"]=>
object(stdClass)#14 (1) {
["description"]=>
object(stdClass)#15 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(457)
["friends_count"]=>
int(500)
["listed_count"]=>
int(3)
["created_at"]=>
string(30) "Fri Nov 23 10:56:08 +0000 2012"
["favourites_count"]=>
int(129)
["utc_offset"]=>
int(19800)
["time_zone"]=>
string(7) "Chennai"
["geo_enabled"]=>
bool(false)
["verified"]=>
bool(false)
["statuses_count"]=>
int(3230)
["lang"]=>
string(2) "en"
["contributors_enabled"]=>
bool(false)
["is_translator"]=>
bool(false)
["is_translation_enabled"]=>
bool(false)
["profile_background_color"]=>
string(6) "0099B9"
["profile_background_image_url"]=>
string(48) "http://abs.twimg.com/images/themes/theme4/bg.gif"
["profile_background_image_url_https"]=>
string(49) "https://abs.twimg.com/images/themes/theme4/bg.gif"
["profile_background_tile"]=>
bool(false)
["profile_image_url"]=>
string(99) "http://pbs.twimg.com/profile_images/3788093993/0639a4b8297f9eea2cbb9de89f5c3a40_normal.jpeg"
["profile_image_url_https"]=>
string(100) "https://pbs.twimg.com/profile_images/37883993/0639a4b8297f9eea2cbb95c3a40_normal.jpeg"
["profile_banner_url"]=>
string(58) "https://pbs.twimg.com/profile_banners/9658922/1386619"
["profile_link_color"]=>
string(6) "0099B9"
["profile_sidebar_border_color"]=>
string(6) "5ED4DC"
["profile_sidebar_fill_color"]=>
string(6) "95E8EC"
["profile_text_color"]=>
string(6) "3C3940"
["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"]=>
NULL
["coordinates"]=>
NULL
["place"]=>
NULL
["contributors"]=>
NULL
["retweeted_status"]=>
object(stdClass)#16 (24) {
["metadata"]=>
object(stdClass)#17 (2) {
["result_type"]=>
string(6) "recent"
["iso_language_code"]=>
string(2) "en"
}
["created_at"]=>
string(30) "Wed Feb 12 15:33:36 +0000 2014"
["id"]=>
float(4.3362493936345E+17)
["id_str"]=>
string(18) "43362493446784"
["source"]=>
string(84) "Twitter for Android"
["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)#18 (40) {
["id"]=>
int(168362964)
["id_str"]=>
string(9) "168362964"
["name"]=>
string(5) "usracc"
["screen_name"]=>
string(12) "useracc"
["location"]=>
string(16) "India."
["url"]=>
NULL
["entities"]=>
object(stdClass)#19 (1) {
["description"]=>
object(stdClass)#20 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(651)
["friends_count"]=>
int(197)
["listed_count"]=>
int(6)
["created_at"]=>
string(30) "Mon Jul 19 03:54:15 +0000 2010"
["favourites_count"]=>
int(4267)
["utc_offset"]=>
int(19800)
["time_zone"]=>
string(7) "Chennai"
["geo_enabled"]=>
bool(true)
["verified"]=>
bool(false)
["statuses_count"]=>
int(10177)
["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(75) "http://pbs.twimg.com/profile_images/4338561184/quFHKeYq_normal.jpeg"
["profile_image_url_https"]=>
string(76) "https://pbs.twimg.com/profile_images/433856131184/quFHKeYq_normal.jpeg"
["profile_banner_url"]=>
string(58) "https://pbs.twimg.com/profile_banners/168364/1392170036"
["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(5)
["favorite_count"]=>
int(10)
["entities"]=>
object(stdClass)#21 (5) {
["hashtags"]=>
array(1) {
[0]=>
object(stdClass)#22 (2) {
["text"]=>
string(7) "v57"
["indices"]=>
array(2) {
[0]=>
int(76)
[1]=>
int(84)
}
}
}
["symbols"]=>
array(0) {
}
["urls"]=>
array(0) {
}
["favorited"]=>
bool(false)
["retweeted"]=>
bool(false)
["possibly_sensitive"]=>
bool(false)
["lang"]=>
string(2) "en"
}
["retweet_count"]=>
int(5)
["favorite_count"]=>
int(0)
Now, i would like to retrieve the original tweets alone which has V57 tag by ignoring all Retweets similar to this
string(131) "RT #useravv: Plz consult a doctor #V57"
I've googled and found this thread. But it was old API version.
My Doubt has been cleared. Thanks to the SO user Newbi3 :)
Here is a way to exclude Retweets in Search api 1.1
$search_tim = $connection->get('search/tweets',array('q' => ' #V57 -RT', 'count' => 5,'result_type' => 'recent'));
Adding -RT at the end of search term will filter it.
EDIT :
The above approach will simply eliminate the tweet too if they included #RT is in their status and username or screename. This will blindly ignore tweets when its sees RT. This is a temporary solution.
The permanent solution is, get the RT status & if if its retweet simply assign a value like 1 to a variable & 0 if its original. Eg..
if (isset($tweet_object->retweeted_status)) {
// This is a retweet
// Use the original tweet's entities, they are more complete
$entities = $tweet_object->retweeted_status->entities;
$is_rt = 1; // Set 1 if retweeted
} else {
$entities = $tweet_object->entities;
$is_rt = 0; // Set 0 if original
}
Then just do like,
if($is_rt==0) // if its a original tweet
{
...
....
}
I have found concatenating "-filter:retweets" to the end of your query string works best.
If you simply add -RT you might run into issues with accidentally filtering tweets containing "RT" for example "Russia Today" will use that string quite a lot!
The following pages describe Twitter search operators:
https://support.twitter.com/articles/71577-using-advanced-search
http://thesocialchic.com/2013/04/26/how-to-master-twitter-search/

How can I access the images in an Instagram JSON? Use objects or an associative array?

Since I stumbled over some wrong solutions (at least always pointing into the right direction) for having your instagram photos pulled onto my site, I started looking for new ways. And I had to implement SSL into the cURL scripting part to get the instagram feed.
However I am stuck in the following part: How can I tell my script to show my images?
I found out, that $result->data works, but if I continue this line with ->images PHP gives me the "Trying to get property of non-object". And the output of the decoded JSON is still lacking overviewability. I pasted here the code as far as I am right now, debugging mode.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/v1/users/myUserID/media/recent/?access_token=myAccessToken');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
$data=($result->data);
var_dump ($data->images);
// echo "<img src="$result->images->thumbnail->url">"
?>
Here is the output of the json_decode:
array(2) {
[0]=> object(stdClass)#4 (15) {
["attribution"]=> NULL
["tags"]=> array(4) {
[0]=> string(5) "remix"
[1]=> string(9) "trncfrmcn"
[2]=> string(4) "tygn"
[3]=> string(9) "mastering"
}
["type"]=> string(5) "image"
["location"]=> NULL
["comments"]=> object(stdClass)#5 (2) {
["count"]=> int(0)
["data"]=> array(0) { }
}
["filter"]=> string(7) "Toaster"
["created_time"]=> string(10) "1373149059"
["link"]=> string(34) "http://instagram.com/p/bcOOGMF5Vi/"
["likes"]=> object(stdClass)#6 (2) {
["count"]=> int(6)
["data"]=> array(4) {
[0]=> object(stdClass)#7 (4) {
["username"]=> string(13) "itsallaboutva"
["profile_picture"]=> string(77)
"http://images.ak.instagram.com/profiles/profile_186336970_75sq_1341852049.jpg"
["id"]=> string(9) "186336970"
["full_name"]=> string(16) "Victoria Anwuzia"
}
[1]=> object(stdClass)#8 (4) {
["username"]=> string(15) "eatmybeatsordie"
["profile_picture"]=> string(76)
"http://images.ak.instagram.com/profiles/profile_32709002_75sq_1355602472.jpg"
["id"]=> string(8) "32709002"
["full_name"]=> string(12) "D-Mite Beats"
}
[2]=> object(stdClass)#9 (4) {
["username"]=> string(12) "joetranmusic"
["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_32545994_75sq_1362449260.jpg"
["id"]=> string(8) "32545994"
["full_name"]=> string(8) "Joe Tran"
}
[3]=> object(stdClass)#10 (4) {
["username"]=> string(14) "xariahmarshall"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_275171230_75sq_1362937374.jpg"
["id"]=> string(9) "275171230"
["full_name"]=> string(14) "xariahmarshall"
}
}
}
["images"]=> object(stdClass)#11 (3) {
["low_resolution"]=> object(stdClass)#12 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_6.jpg"
["width"]=> int(306)
["height"]=> int(306)
}
["thumbnail"]=> object(stdClass)#13 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_5.jpg"
["width"]=> int(150)
["height"]=> int(150)
}
["standard_resolution"]=> object(stdClass)#14 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_7.jpg"
["width"]=> int(612)
["height"]=> int(612)
}
}
["users_in_photo"]=> array(0) { }
["caption"]=> object(stdClass)#15 (4) {
["created_time"]=> string(10) "1373149155"
["text"]=> string(34) "#Mastering #TYGN #TRNCFRMCN #Remix"
["from"]=> object(stdClass)#16 (4) {
["username"]=> string(12) "liebdich9vol"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["id"]=> string(9) "444541508"
["full_name"]=> string(11) "Ian Maybach"
}
["id"]=> string(18) "494333402642093342"
}
["user_has_liked"]=> bool(false)
["id"]=> string(28) "494332600473064802_444541508"
["user"]=> object(stdClass)#17 (6) {
["username"]=> string(12) "liebdich9vol"
["website"]=> string(0) ""
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["full_name"]=> string(11) "Ian Maybach"
["bio"]=> string(0) ""
["id"]=> string(9) "444541508"
}
}
[1]=> object(stdClass)#18 (15) {
["attribution"]=> NULL
["tags"]=> array(3) {
[0]=> string(6) "studio"
[1]=> string(6) "garden"
[2]=> string(4) "view"
}
["type"]=> string(5) "image"
["location"]=> NULL
["comments"]=> object(stdClass)#19 (2) {
["count"]=> int(0)
["data"]=> array(0) { }
}
["filter"]=> string(7) "Toaster"
["created_time"]=> string(10) "1372661045"
["link"]=> string(34) "http://instagram.com/p/bNrZ-sl5Xp/"
["likes"]=> object(stdClass)#20 (2) {
["count"]=> int(4)
["data"]=> array(4) {
[0]=> object(stdClass)#21 (4) {
["username"]=> string(9) "seanbabas"
["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_30025104_75sq_1369077409.jpg"
["id"]=> string(8) "30025104"
["full_name"]=> string(10) "Sean Babas"
}
[1]=> object(stdClass)#22 (4) {
["username"]=> string(12) "ginocolletti"
["profile_picture"]=> string(75) "http://images.ak.instagram.com/profiles/profile_4634400_75sq_1364847565.jpg"
["id"]=> string(7) "4634400"
["full_name"]=> string(13) "Gino Colletti"
}
[2]=> object(stdClass)#23 (4) {
["username"]=> string(13) "marygraceklee"
["profile_picture"]=> string(75) "http://images.ak.instagram.com/profiles/profile_3269702_75sq_1373251095.jpg"
["id"]=> string(7) "3269702"
["full_name"]=> string(13) "Marygrace Lee"
}
[3]=> object(stdClass)#24 (4) {
["username"]=> string(10) "cyonthefly"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_225204950_75sq_1368426695.jpg"
["id"]=> string(9) "225204950"
["full_name"]=> string(21) "!CY ÊŽlÉŸÇɥʇuo ✈"
}
}
}
["images"]=> object(stdClass)#25 (3) {
["low_resolution"]=> object(stdClass)#26 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_6.jpg"
["width"]=> int(306)
["height"]=> int(306)
}
["thumbnail"]=> object(stdClass)#27 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_5.jpg"
["width"]=> int(150)
["height"]=> int(150)
}
["standard_resolution"]=> object(stdClass)#28 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_7.jpg"
["width"]=> int(612)
["height"]=> int(612)
}
}
["users_in_photo"]=> array(0) { }
["caption"]=> object(stdClass)#29 (4) {
["created_time"]=> string(10) "1372900093"
["text"]=> string(21) "#studio #view #garden"
["from"]=> object(stdClass)#30 (4) {
["username"]=> string(12) "liebdich9vol"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["id"]=> string(9) "444541508"
["full_name"]=> string(11) "Ian Maybach"
}
["id"]=> string(18) "492244118468531915"
}
["user_has_liked"]=> bool(false)
["id"]=> string(28) "490238835760272873_444541508"
["user"]=> object(stdClass)#31 (6) {
["username"]=> string(12) "liebdich9vol"
["website"]=> string(0) ""
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["full_name"]=> string(11) "Ian Maybach"
["bio"]=> string(0) ""
["id"]=> string(9) "444541508"
}
}
}
My aim is to have only one picture on my site but navigation arrows to switch to the next one. How would that be achieved?
To access infos from StdClass you can do this:
foreach($data->images as $i){
$i->url;
}
You can force a casting to array too:
foreach($data->images as $i){
$i = $i->toArray();
$i['url'];
}
And you can get the json decode on array form:
$data = json_decode($json,true); //the second param true means, bring me it like array.
as per http://php.net/json_decode, you need to specify you want an associative array instead of an object from json_decode:
so use json decode like this
$result=json_decode($result, true);
Now you will get array and you can easily access data.

Categories