PHP getting Twitter API JSON file contents without OAuth (Almost have it)
Hey guys, I have this script working fine with OAuth, but I accidentally nuked my 350 API hits with a stupid while statement :( I'm trying to get data from the Twitter API without OAuth, I can't figure it out (still pretty new), heres what I have
<html>
<body>
<center>
<hr />
<br />
<table border="1">
<tr><td>ScreenName</td><td>Followed back?</td></tr>
<?php
//twitter oauth deets
$consumerKey = 'x';
$consumerSecret = 'x';
$oAuthToken = 'x';
$oAuthSecret = 'x';
// Create Twitter API objsect
require_once("twitteroauth.php");
$oauth = new TwitterOAuth($consumerKey, $consumerSecret, $o开发者_开发问答AuthToken, $oAuthSecret);
//get home timeline tweets and it is stored as an array
$youfollow = $oauth->get('http://api.twitter.com/1/friends/ids.json?screen_name=lccountdown');
$i = 0;
//start loop to print our results cutely in a table
while ($i <= 20){
$youfollowid = $youfollow[$i];
$resolve = "http://api.twitter.com/1/friendships/exists.json?user_a=".$youfollow[$i]."&user_b=jwhelton";
$followbacktest = $oauth->get($resolve);
//$homedate= $hometimeline[$i]->created_at;
//$homescreenname = $hometimeline[$i]->user->screen_name;
echo "<tr><td>".$youfollowid."</td><td>".$followbacktest."</td></tr>";
$i++;
}
?>
</table>
</center>
</body>
</html>
Neither of the two Twitter functions require authentication, so how can I get the same results? Thanks guys, Dex
You aren't calling making the TwitterOAuth calls correctly. Don't use the full URL just the method path and pass parameters in a hash array.
You probably also don't want to use a while loop when a foreach will work just as well.
<?php
//twitter oauth deets
$consumerKey = 'x';
$consumerSecret = 'x';
$oAuthToken = 'x';
$oAuthSecret = 'x';
// Create Twitter API objsect
require_once("twitteroauth.php");
$oauth = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//get home timeline tweets and it is stored as an array
$youfollow = $oauth->get('friends/ids', array('screen_name' => 'lccountdown'));
$i = 0;
//start loop to print our results cutely in a table
foreach($youfollow as $id){
$followbacktest = $oauth->get('friendships/exists', array('user_a' => $id, 'user_b' => 'jwhelton'));
echo "<tr><td>".$id."</td><td>".$followbacktest."</td></tr>";
}
I have not directly tested this but it should work. You should also consider moving to fuller featured GET friendships/show method.
精彩评论