[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming > CODE

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.


Closed Thread
 
LinkBack Thread Tools
Old 08-06-2007, 03:28 PM   #1 (permalink)
Stud Sausage
 
Join Date: Dec 2006
Location: England
Posts: 1,546
34.41 NP$ (Donate)

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.
                
    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($curl, CURLOPT_URL, $this -> twitter_base . 'update.xml?status=' . stripslashes(urlencode($message)));
            
// The variable could also be substituded by the use of CURLOPT_POSTFIELDS
            
        
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            
// Enables us to return the result as a string instead of outputting to browser.
            
        
curl_setopt($curl, CURLOPT_POST, 1);
            
// From the Twitter API docs: "Request must be a POST". CURLOPT_POST ensures this./
            
        
$username = $this -> twitter_username;
        
$pass = $this -> twitter_password;
            
        
curl_setopt($curl, CURLOPT_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($curl, CURLINFO_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
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline  
Closed Thread


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 09:14 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85