iBrightDev
June 28th, 2009, 11:53
well, i was wanting to past the most recent @reply from my account to a website, but, coudnt seem to find anything out there for it. i found hundreds of things about posting the most recent user comments, but, that isnt what i wanted. so, i started messing with the things i found and eventually got my script to return the most recent @reply. but, then i ran into another obstacle, logging into the twitter api as a valid user. well, i figured CURL would be able to solve this issue, but, i had never used CURL and was not to sure how to go about that either. so, more googleing had to take place. long story short, i finally got curl working and was able to do what i wanted and figured i would share it with my fellow FWSers in case anyone was wanting something like this.
$glbl = new specialIncludes();
$twtr = new twitter("YOUR TWITTER USERNAME HERE", "YOUR TWITTER PASSWORD HERE");
// GLOBAL FUNCTIONS
class specialIncludes {
public function trunc($phrase, $url, $limit, $target) {
global $bL;
if ($target != null) {
$target = ' target="_' . $target . '"';
}
$max_words = $limit;
$phrase_array = explode(' ',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'<a href="' . $url . '"' . $target . '>...</a>';
return $phrase;
}
}
class twitter {
private $Username;
private $Password;
private $UrlTwitter = 'http://twitter.com/';
private $UrlStatus = 'http://twitter.com/statuses/';
function __construct ($user, $password) {
$this->Username = $user;
$this->Password = $password;
}
public function replies($count) {
global $glbl;
$url = 'http://twitter.com/statuses/replies/' . $this->Username . '.xml?callback=twitterCallback2&count=' . $count;
if(function_exists(curl_init)) {
//Create the connection handle
$ch = curl_init($url);
//Set cURL options
curl_setopt($ch, CURLOPT_URL, $url); //URL to connect to
curl_setopt($ch, CURLOPT_GET, 1); //Use GET method
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication
curl_setopt($ch, CURLOPT_USERPWD, "$this->Username:$this->Password"); //Set u/p
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate (but use SSL of course), live dangerously!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return the result as string
// Result from querying URL. Will parse as xml
$output = curl_exec($ch);
$Headers = curl_getinfo($ch);
if($Headers['http_code'] == 200) {
$data = simplexml_load_string($output);
$protc = "protected";
$twtr_user_id = (string) $data->status->user->id;
$twtr_user_name = (string) $data->status->user->name;
$twtr_user_screen_name = (string) $data->status->user->screen_name;
$twtr_user_location = (string) $data->status->user->location ;
$twtr_user_description = (string) $data->status->user->description;
$twtr_user_pimgurl = (string) $data->status->user->profile_image_url;
$twtr_user_url = (string) $data->status->user->url;
$twtr_status_created_at = (string) $data->status->created_at;
$twtr_status_id = (string) $data->status->id;
$twtr_status_text = (string) $data->status->text;
$twtr_status_source = (string) $data->status->source;
// Fri Jun 05 10:46:56 +0000 2009
$twtr_post_date = $twtr_status_created_at;
list($wk_day, $month, $day, $day_time, $plus_time, $year) = split(" ", $twtr_post_date);
$twtr_post_date = "$month $day, $year";
// Build a pattern that is going to be used in a @reply replace
$pattern = "#(^|[\n ])@([^ \"\t\n\r<]*)#ise";
// Build the link for the @reply replace
$replace = "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'";
// Find instances of a user @reply, and replace it with a link
$twtr_status_text = preg_replace($pattern, $replace, $twtr_status_text);
// Build a url for a more link
$twtr_more_link = 'http://twitter.com/' . $twtr_user_screen_name . '/statuses/' . $twtr_status_id;
// Truncate the text if needed
$twtr_status_text = $glbl -> trunc($twtr_status_text, $twtr_more_link, 25, 'blank');
$tweet .= '<span class="small"><strong>' . $twtr_user_screen_name . ' said:</strong> </span>'.$bL;
$tweet .= '<span class="small">' . $twtr_status_text . '</span> <a style="font-size:85%; display: none;" href="http://twitter.com/' . $twtr_user_screen_name . '/statuses/' . $twtr_status_id . '">' . $twtr_post_date . '</a>';
return $tweet;
} else {
if ($Headers['http_code'] == 401) {
$this->Error(4);
} elseif ($Headers['http_code'] == 404) {
$this->Error(5);
}
}
// close cURL resource.
curl_close($ch);
}
} // END LOG_API
private function Error($Error) {
// List of Errors
$e[1] = 'Username and/or password not set';
$e[2] = 'CURL library not installed';
$e[3] = 'Post value too long/not set';
$e[4] = 'Invalid username/password';
$e[5] = 'Invalud URL for CURL request';
$e[6] = 'Invalid ID value entered';
$e[7] = 'You are not authorized to view this page';
$e[8] = 'All variables for requested function not set';
$e[9] = 'For and/or Message not set';
// Display Error
if (array_key_exists($Error, $e)){
echo $e[$Error];
} else {
echo 'Invalid Error Code';
}
} // End Error()
}
here is the html i used to utilize the twitter class i created.
<div class="float-left btm-txt">
<!-- TWITTER -->
<div id="twitter_div">
<ul id="twitter_update_list">
<li class="small"><?=$twtr->replies(1)?></li>
</ul>
<div class="follow_us"><a href="http://twitter.com/YOUR TWITTER USERNAME HERE" title="Follow me on Twitter" target="Twitter">Follow us on twitter</a></div>
</div>
<!-- END TWITTER -->
</div>
now, there is one problem that i have run into, and maybe someone will post a solution before i get around to figuring it out myself.
in this little bit of code...
<?=$twtr->replies(1)?>
we can change the "1" to a "3", no quotes, and have it pull more results in the xml, but, i cant seem to get it to spit out the other results, just the first one. now exactly sure why. so, if anyone knows what i need to add to the script, please, let me know.
right now, you can see the code in action here...
demo here (http://dynamicpagesolutions.com/new/justin/)
$glbl = new specialIncludes();
$twtr = new twitter("YOUR TWITTER USERNAME HERE", "YOUR TWITTER PASSWORD HERE");
// GLOBAL FUNCTIONS
class specialIncludes {
public function trunc($phrase, $url, $limit, $target) {
global $bL;
if ($target != null) {
$target = ' target="_' . $target . '"';
}
$max_words = $limit;
$phrase_array = explode(' ',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'<a href="' . $url . '"' . $target . '>...</a>';
return $phrase;
}
}
class twitter {
private $Username;
private $Password;
private $UrlTwitter = 'http://twitter.com/';
private $UrlStatus = 'http://twitter.com/statuses/';
function __construct ($user, $password) {
$this->Username = $user;
$this->Password = $password;
}
public function replies($count) {
global $glbl;
$url = 'http://twitter.com/statuses/replies/' . $this->Username . '.xml?callback=twitterCallback2&count=' . $count;
if(function_exists(curl_init)) {
//Create the connection handle
$ch = curl_init($url);
//Set cURL options
curl_setopt($ch, CURLOPT_URL, $url); //URL to connect to
curl_setopt($ch, CURLOPT_GET, 1); //Use GET method
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication
curl_setopt($ch, CURLOPT_USERPWD, "$this->Username:$this->Password"); //Set u/p
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate (but use SSL of course), live dangerously!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return the result as string
// Result from querying URL. Will parse as xml
$output = curl_exec($ch);
$Headers = curl_getinfo($ch);
if($Headers['http_code'] == 200) {
$data = simplexml_load_string($output);
$protc = "protected";
$twtr_user_id = (string) $data->status->user->id;
$twtr_user_name = (string) $data->status->user->name;
$twtr_user_screen_name = (string) $data->status->user->screen_name;
$twtr_user_location = (string) $data->status->user->location ;
$twtr_user_description = (string) $data->status->user->description;
$twtr_user_pimgurl = (string) $data->status->user->profile_image_url;
$twtr_user_url = (string) $data->status->user->url;
$twtr_status_created_at = (string) $data->status->created_at;
$twtr_status_id = (string) $data->status->id;
$twtr_status_text = (string) $data->status->text;
$twtr_status_source = (string) $data->status->source;
// Fri Jun 05 10:46:56 +0000 2009
$twtr_post_date = $twtr_status_created_at;
list($wk_day, $month, $day, $day_time, $plus_time, $year) = split(" ", $twtr_post_date);
$twtr_post_date = "$month $day, $year";
// Build a pattern that is going to be used in a @reply replace
$pattern = "#(^|[\n ])@([^ \"\t\n\r<]*)#ise";
// Build the link for the @reply replace
$replace = "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'";
// Find instances of a user @reply, and replace it with a link
$twtr_status_text = preg_replace($pattern, $replace, $twtr_status_text);
// Build a url for a more link
$twtr_more_link = 'http://twitter.com/' . $twtr_user_screen_name . '/statuses/' . $twtr_status_id;
// Truncate the text if needed
$twtr_status_text = $glbl -> trunc($twtr_status_text, $twtr_more_link, 25, 'blank');
$tweet .= '<span class="small"><strong>' . $twtr_user_screen_name . ' said:</strong> </span>'.$bL;
$tweet .= '<span class="small">' . $twtr_status_text . '</span> <a style="font-size:85%; display: none;" href="http://twitter.com/' . $twtr_user_screen_name . '/statuses/' . $twtr_status_id . '">' . $twtr_post_date . '</a>';
return $tweet;
} else {
if ($Headers['http_code'] == 401) {
$this->Error(4);
} elseif ($Headers['http_code'] == 404) {
$this->Error(5);
}
}
// close cURL resource.
curl_close($ch);
}
} // END LOG_API
private function Error($Error) {
// List of Errors
$e[1] = 'Username and/or password not set';
$e[2] = 'CURL library not installed';
$e[3] = 'Post value too long/not set';
$e[4] = 'Invalid username/password';
$e[5] = 'Invalud URL for CURL request';
$e[6] = 'Invalid ID value entered';
$e[7] = 'You are not authorized to view this page';
$e[8] = 'All variables for requested function not set';
$e[9] = 'For and/or Message not set';
// Display Error
if (array_key_exists($Error, $e)){
echo $e[$Error];
} else {
echo 'Invalid Error Code';
}
} // End Error()
}
here is the html i used to utilize the twitter class i created.
<div class="float-left btm-txt">
<!-- TWITTER -->
<div id="twitter_div">
<ul id="twitter_update_list">
<li class="small"><?=$twtr->replies(1)?></li>
</ul>
<div class="follow_us"><a href="http://twitter.com/YOUR TWITTER USERNAME HERE" title="Follow me on Twitter" target="Twitter">Follow us on twitter</a></div>
</div>
<!-- END TWITTER -->
</div>
now, there is one problem that i have run into, and maybe someone will post a solution before i get around to figuring it out myself.
in this little bit of code...
<?=$twtr->replies(1)?>
we can change the "1" to a "3", no quotes, and have it pull more results in the xml, but, i cant seem to get it to spit out the other results, just the first one. now exactly sure why. so, if anyone knows what i need to add to the script, please, let me know.
right now, you can see the code in action here...
demo here (http://dynamicpagesolutions.com/new/justin/)