NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Twitter API - Fetch status and post to twitter via PHP

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-06-2007, 04:28 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,568
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship

Twitter API - Fetch status and post to twitter via PHP


This is the start of a script I made following a tutorial I wrote (currently pending) on pixel2life's publishing system about grabbing your twitter status.

I may decide to add more features in time as there is a large scope to add more things however this is what I done quickly for my own site:

-----------------------

If you want to post about the code elsewhere like on your own blog please link to this topic and http://mediascratch.com, that is all I ask. You are not required to place credit if you use the code, it is entirely free and of course you are welcome to modify it providing initial credits remain with my name and website.

PHP Code:
<?php

/*   This class is compatible with >= PHP 5 only due to the use of:
    SimpleXML:  http://uk2.php.net/simplexml
                Used to parse XML feeds 
                SimpleXML is enabled by default. If it appears not to be, contact your host.
                
????: NamePros.com http://www.namepros.com/code/358587-twitter-api-fetch-status-post-twitter.html
    cURL is also required for this script to operate. http://uk.php.net/curl
    
    The Twitter API documentation used to create this class can be found:
    http://groups.google.com/group/twitter-development-talk/web/api-documentation
    
    Written by Matt Jewell
    http://mediascratch.com
*/

class twitterAPI {
 
    public  
$twitter_base         'http://twitter.com/statuses/';
    private 
$twitter_username     'your username';
    private 
$twitter_password     'your password';
    
    

    public function 
fetch_latest_status() {
        
        
/*  user_timeline
            Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user.  It's also possible to request another user's timeline via the id parameter below.        
        
            Parameters used: id 
                             count  
        */
        
        
$buffer file_get_contents($this -> twitter_base 'user_timeline/' $this -> twitter_username '.xml?count=1');
                
// Grab the contents of the XML file and store it in the variable.
                
        
$xml    = new SimpleXMLElement($buffer);
                
// Creating a new XML string for use with the SimpleXML extension.
                
        
$status_item $xml -> status;
                
// Creating a new variable using SimpleXML with "status" as the Node.
        
return $status_item -> text;
                
// Grab the current status as the Element.
    
}
    
    

    function 
update_status($message){
    
        
        
$curl curl_init();
    
        
curl_setopt($curlCURLOPT_URL$this -> twitter_base 'update.xml?status=' stripslashes(urlencode($message)));
            
// The variable could also be substituded by the use of CURLOPT_POSTFIELDS
            
        
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
            
// Enables us to return the result as a string instead of outputting to browser.
????: NamePros.com http://www.namepros.com/showthread.php?t=358587
            
        
curl_setopt($curlCURLOPT_POST1);
            
// From the Twitter API docs: "Request must be a POST". CURLOPT_POST ensures this./
            
        
$username $this -> twitter_username;
        
$pass $this -> twitter_password;
            
        
curl_setopt($curlCURLOPT_USERPWD"$username:$pass");
            
// Authenticates the user (you) in the post request.   

        
        
$exec curl_exec($curl);
            
// Execute the cURL request.
            
        // echo curl_error($curl); 
        // Used to debug cURL.
        
        
$httpcode curl_getinfo($curlCURLINFO_HTTP_CODE);
            
// Return the HTTP headers so we can confirm the request.
            // A list of what the status codes are and mean can be found here:
            // http://www.seoconsultants.com/tools/headers.asp#code-200    
        
        
curl_close($curl);
            
// Terminate the cURL session
    
        
return ($httpcode == 200) ? 'Updated!' 'Error!';
            
/* Ternary operator equivelant to:
            
               IF httpcode equals 200 
                       Return done
               ELSE 
                       Return error    
            */
            
    
}
}
?>
Enjoy

Matt
Matthew. is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 05:50 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger