views:

252

answers:

2

I use the below code to post updates to twitter:

// Set username and password for twitter API
     $username = '***';
     $password = '***';

     // The twitter API address
     $url = 'http://twitter.com/statuses/update.xml';

     // Alternative JSON version
     // $url = 'http://twitter.com/statuses/update.json';

     // Set up and execute the curl process
     $curl_handle = curl_init();

     curl_setopt($curl_handle, CURLOPT_URL, "$url");
     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl_handle, CURLOPT_POST, 1);
     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$update");
     curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");

     $buffer = curl_exec($curl_handle);

     curl_close($curl_handle);

     // check for success or failure

     if (empty($buffer)) 
     {
         echo 'error?!';
     } 
     else 
     {
         // mark the song as tweetered
      mysql_query("UPDATE `songs` SET `tweet` = '1' WHERE `id` = $songid ");
     }
    }

    echo $update;

The post functions however it is cut off for some reason.

For example the above code will post: Tiesto - Feel It In My Bones (Feat. Tegan to twitter.

When it should post: Tiesto - Feel It In My Bones (Feat. Tegan & Sara) - 160Kbps http://bit.ly/5mdCK4

I use the echo $update; line at the end compare the output to twitter to the $update value and $update is correct.

+3  A: 

Have you tried other input strings? From what you've posted there, I'd bet that your problem is the ampersand in the status, since that's a metacharacter in form posts. I'm sure PHP has some function to URL-encode strings, and you'll want to use it on $update.

Sixten Otto
ian
ian
Why repost? It's answered here.
ceejayoz
+1  A: 

Sixten Otto is right. You need to do:

$update =urlencode($update);

to prevent issues with special characters.

ceejayoz
Oh, hey, it's even named something appropriate! :-)
Sixten Otto