Article

Share Media on Twitter Using Flex, Part I: The Basics

Page: 1 2 3 4 5 Next

Since we’ll be expecting the Flash movie to use POST to send us the data we need, we may wind up receiving some unwanted backslashes (\); this can occur when magic_quotes_gpc is configured in your copy of PHP. Magic quotes are now deprecated in PHP, but there are still plenty of web hosts out there with them switched on, so it’s always worth checking for this configuration when you accept POST submissions. Here’s a simple function to strip slashes in case magic quotes are enabled:

function tidyslashes($text) {  
 if (get_magic_quotes_gpc()) {  
   $cleantext = stripslashes($text);  
 }  
 else {  
   $cleantext = $text;  
 }  
 return $cleantext;  
}

We’ll use that function to clean up the tweet-related variables we expect to receive, which are declared as global variables:

$username = tidyslashes($_POST['username']);  
$password = tidyslashes($_POST['password']);  
$message = tidyslashes($_POST['message']);

We’ll also need three functions—one to update, one to retrieve tweets from the user’s friends, and one to view the public timeline. Each function is fairly similar; we’ll just take a look at one for now. Here’s the update function:

function update() {  
 global $username, $password, $message;  
 $twitter = new Twitter($username, $password);  
 $update = $twitter->updateStatus($message);  
 print_r($update);  
}

What’s happening here? First, we grab the username, password, and message variables. Next, we specify a new instance of the Twitter class from twitterlibphp, passing the username and password along. Third, we specify the variable update as the result of the Twitter class’ updateStatus function, and simply print the result, which will be in XML format.

Additionally, we’ll have received a variable, named type, from the Flash movie to tell us which action we want to perform. We’ll use a switch statement to execute the appropriate function. If type was not specified, or says anything other than update, friends, or public, the script will die with a message:

switch($_POST['type']) {  
 case 'update':  
    update();  
    break;  
 case 'public':  
    getpublictimeline();  
    break;  
 case 'friends':  
    getfriends();  
    break;  
 default:  
    die("died: not called properly");  
    break;  
}

You can view the script in full here, and there’s a copy in the code archive too.

Copy the completed script, along with twitter.lib.php to a web server, and note the URL for later—we’ll need to tell our Flash application where this lives. If you downloaded our version of the script, you’ll find a very basic form you can use to test the updates function of your proxy. If all went well, you should be able to post to a Twitter account using that form!

If your web server is not running locally, your next step is to include a cross-domain policy file on your web server, so that it’s easier to run the app later. Here’s a permissive example policy file that will permit access from anywhere:

<?xml version="1.0"?>  
<!DOCTYPE cross-domain-policy  
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">  
<cross-domain-policy>  
 <site-control permitted-cross-domain-policies="all" />  
 <allow-access-from domain="*" />  
 <allow-http-request-headers-from domain="*" headers="*" secure="false"/>  
</cross-domain-policy>

You’ll need to upload that file to the root of your domain, and name it crossdomain.xml.

Now that we’ve prepared this proxy, we can begin to add functionality to our Flash project and start testing.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article