How do I show all my Twitter friends? - php

This script isn't working so good.
<?php
//include twitter class
require('classes/twitter.php');
$twitter = new Twitter('custToken', 'custSecret');
// set tokens
$twitter->setOAuthToken('blah blah');
$twitter->setOAuthTokenSecret('blah blah');
//$tweet = "This tweet was posted from a custom php script.";
//$twitter->statusesUpdate($tweet); // <-- this works!
$friends = $twitter->friendsList('tynamite');
foreach ($friends as $friend){
print_r($friend);
echo "<hr>";
}
?>
I want to display all my Twitter friends (everyone I follow) in a list.
The result I am getting is this.
Could anyone please help on this?

You should pass a cursor. You could use something like the code below (untested and just pseudo-code):
while($count == 20) {
$friends = $twitter->friendsList('tynamite', $cursor);
$count = count($friends);
$cursor++;
.. your code ..
}

twitter uses json to return its results. you need to parse the results returned and pull out the information you need. check this blog out for an example of how to use php to parse json

Related

Right way of browsing Instagram API result array of images with foreach

I've been recently building a plugin for wordpress which basically uses the instagram API to get an image URL and then place it in a short code.
And I've come to a problem.
I get this error:
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at
line 22
and I have no idea what am I doing wrong.
My code for the foreach:
//define Access token
$accesst= "ACCESS_TOKEN_GOES_HERE";
//userid
$userid=USERID_GOES_HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'][0]['images']['standard_resolution']['url'] as $photo)
{
print $photo['url'][0];
echo "<br>";
}
My JSON var_dump got me this:
https://pastebin.com/3RaL6EUA
The access codes, were of course deleted before posting this.
Does anyone have a clue what I'm doing wrong?
EDIT:
Thanks, everyone, got it figured out in the comments.
Your $standardres['data'] have items which have images, so you must use $standardres['data'] in the foreach loop and then parse the image url from the item data.
foreach($standardres['data'] as $item) {
print $item['images']['standard_resolution']['url'];
echo "<br>";
}
I don't know exactly the hierarchy of the instagram API, but I suggest you to try :
foreach($standardres['data']['images'] as $photo) {
print_r($photo); // to see what the array contains!
echo $photo['standard_resolution']['url'];
echo "<br>";
}

Twitch API and PHP(View Streamer Data)

I'm trying to find the best way to pull data about users down to display on my site. In the long run, the usernames will be coming from a MYSQL database, but for now I just created a small array. For testing purposes, I would love for it to spit out (username from array) and viewer count. But I'm having no luck.
Here is my code below, the code below I was just trying to output anything from the two usernames, but it was coming up empty.
<?php
$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");
foreach($username as $stream) {
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream));
$streamstats[] = $stream;
$print_r($streamstats);
}
?>
Which I get nothing. Any advice?
You have 2 errors. First you have $ before print_r. Second one is that you print it in a foreach loop, but you should print everything after the loop. Give a shot code below:
$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");
foreach($username as $stream) {
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream));
$streamstats[] = $data;
}
print_r($streamstats);

simple_xml_load_string if nothing is returned

I am attempting to only run a loop if xml results actually exist. I am getting the xml results via:
$albums = simplexml_load_string(curl_get($api_url . '/videos.xml'));
What I want to be able to do is that on the next line say:
if($albums = hasAValue())
// Loop
Any ideas? Or a way to check before I load the XML data?
Side note: This is using the Vimeo API.
No, you need to further go down with the resultant with the namespace, reach till body give the xpath and work on.
$albums->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
To be specific, let me know the XML response you are getting i will let you the output.
UPDATED
$albums = simplexml_load_string("#your response#");
echo count($xml->children());
The dirty way:
$albums = #simplexml_load_string(curl_get($api_url . '/videos.xml'));
if ($albums)
{
...
}
This is dirty because of the Error Control Operator # which is used to "deal" with the error cases (e.g. problem fetching the remote location).
The alternative is to differentiate more here:
$xml = curl_get($api_url . '/videos.xml');
$albums = NULL;
if ($xml)
{
$albums = simplexml_load_string($xml);
}
if ($albums)
{
...
}

Retrieving all twitter followers with a single request

I tried this and even added a cursor but it would still retrieve only the first 100 followers.
<?php
$cursor = -1;
$account_from = 'username';
do
{
$json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'.json?cursor=' . $cursor);
$accounts = json_decode($json);
foreach ($accounts->users as $account)
{
$a[] = $account->screen_name ;
}
$cursor = $accounts->next_cursor;
}
while ($cursor > 0);
foreach($a as $f) {
echo $f ;
}
?>
Is there a better and simpler way of doing it? Where am i going wrong? help please?
API docs state this request is deprecated:
This method is deprecated as it will only return information about users who have Tweeted recently. It is not a functional way to retrieve all of a users followers. Instead of using this method use a combination of GET followers/ids and GET users/lookup.
Use this API instead: https://dev.twitter.com/docs/api/1/get/followers/ids
There should be a property in your object called next_cursor, as long as that's non-zero, keep doing the request again (specifying that cursor) until you get all the results.

PHP - Codeigniter twitter loops

new here!!
Im trying to get tweets to display on a site(framework is codeigniter). I am using the twitter api (for example: https://api.twitter.com/1/statuses/user_timeline/ddarrko.xml) to get the tweets and subsequently insert them into a database which I will then get to display on the site. The actual code runs fine the issue is it only ever processes one tweet. my code is -
//get twitter address
$this->load->model('admin_model');
$getadd = $this->admin_model->get_settings("twitter_address");
$twitter_user = $getadd->item_value;
//define twitter xml file
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
foreach ($xml->status as $tweet);
{
echo "<pre>";print_r($xml);echo "</pre>";
$this->data->username=$twitter_user;
$this->data->twitter_status=$tweet->text;
$this->data->pub_date=$tweet->created_at;
//load model and insert tweets;
$this->load->model('tweet_model');
$this->tweet_model->insert_tweets($this->data);
}
as you can see I am defining to run each status in xml file. The echo pre line is me testing because when printing $tweet only one tweet is coming up however even if i loop through just $xml still only one tweet is processed despite there being loads in the file.
any help/advice would be greatly appreciated!
Below code will give you all the available tweets from XML. you can manipulate below logic as per your need.
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
$count_tweet = sizeof($xml);
for($i=0 ; $i < $count_tweet ; $i++)
{
echo"<br>Tweet: ". $xml->status[$i]->text;
echo"<br>date:".$xml->status[$i]->created_at."<br>";
}
Thanks.

Categories