Jump to content


Updating your Twitter status from PHP

- - - - -

  • Please log in to reply
5 replies to this topic

#1
PaulOBrien

PaulOBrien

    It's My Party

  • Founder
  • PipPipPipPipPipPip
  • 32,944 posts
  • Gender:Male
  • Location:Norwich, UK
  • Devices:Galaxy Nexus / TF Prime
  • Twitter:@paulobrien
For one of the interesting projects i've been working on, i've needed to update my Twitter status from within a PHP script... and I thought the code to do it (that uses the Twitter API) might be of interest to others... so here it is :)

<?php

$email = $_GET['email'];
$password = $_GET['password'];
$status = urlencode( $_GET['status'] );

$url = "http://8.7.217.31/statuses/update.xml";

$session = curl_init();
curl_setopt ( $session, CURLOPT_URL, $url );
curl_setopt ( $session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt ( $session, CURLOPT_HEADER, false );
curl_setopt ( $session, CURLOPT_USERPWD, $email . ":" . $password );
curl_setopt ( $session, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $session, CURLOPT_POST, 1);
curl_setopt ( $session, CURLOPT_POSTFIELDS,"status=" . $status);
$result = curl_exec ( $session );
curl_close( $session );

echo( $result );

?>
Simple but effective :D

P

You can follow me on Twitter - http://twitter.com/paulobrien / Follow MoDaCo on Twitter - http://twitter.com/modaco / Follow MoDaCo Android on Twitter - http://twitter.com/modacoandroid

Want to donate? MoDaCo is raising money for the Multiple Sclerosis society.

Posted Image


#2
PaulOBrien

PaulOBrien

    It's My Party

  • Founder
  • PipPipPipPipPipPip
  • 32,944 posts
  • Gender:Male
  • Location:Norwich, UK
  • Devices:Galaxy Nexus / TF Prime
  • Twitter:@paulobrien
Updated to do status post rather than get.

Naughty me!

P

You can follow me on Twitter - http://twitter.com/paulobrien / Follow MoDaCo on Twitter - http://twitter.com/modaco / Follow MoDaCo Android on Twitter - http://twitter.com/modacoandroid

Want to donate? MoDaCo is raising money for the Multiple Sclerosis society.

Posted Image


#3
Jaakko

Jaakko

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I have also played with similar php twitter uploader script but I encountered a problem when using scandinavian characters like ä or ö.

I've encoded the status message characters with the url encoder like this:

$status = urlencode ($status);

However, it doesn't work. The status text which appears in the twitter site is cut from the first scandinavian character. Has someone any proposals how to make this work or is this a bug on twitter side?

Jaakko


#4
PaulOBrien

PaulOBrien

    It's My Party

  • Founder
  • PipPipPipPipPipPip
  • 32,944 posts
  • Gender:Male
  • Location:Norwich, UK
  • Devices:Galaxy Nexus / TF Prime
  • Twitter:@paulobrien
Assuming the $status variable is intact when you go into that line (worth checking), then the urlencode should fix it AFAIK, so perhaps it is something odd Twitter are doing.

I'd suggest dropping them a mail, they're generally pretty good at replying ;)

P

You can follow me on Twitter - http://twitter.com/paulobrien / Follow MoDaCo on Twitter - http://twitter.com/modaco / Follow MoDaCo Android on Twitter - http://twitter.com/modacoandroid

Want to donate? MoDaCo is raising money for the Multiple Sclerosis society.

Posted Image


#5
Jaakko

Jaakko

    Newbie

  • Members
  • Pip
  • 2 posts

View PostPaul (MVP), on Feb 20 2007, 12:39, said:

Assuming the $status variable is intact when you go into that line (worth checking), then the urlencode should fix it AFAIK, so perhaps it is something odd Twitter are doing.

I'd suggest dropping them a mail, they're generally pretty good at replying ;)

P

Ok, I submitted a request via contact page. I'll let you know if they respond.


#6
dropkernel

dropkernel

    Newbie

  • Members
  • Pip
  • 1 posts
Try wrapping the message using utf8_encode() function. This works for me when posting an update using PHP and curl.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users