zondag 5 juli 2009

Plaats berichten via Twitter op je website

Are you not Dutch? But English is no problem? Click here to go to the English version.



Ik ben bezig met Twitter te experimenteren. Een eigen Twitter-account heb ik al, aan dat account zit mijn mobiele telefoon gekoppeld. Dus als ik een bericht stuur, komt het op mijn eigen Twitter binnen.

Nu heb ik voor de drumband ook een Twitter account aangemaakt, wat ik eigenlijk wil gaan gebruiken om berichtjes op onze website toe te voegen. Lijkt me wel leuk om op deze manier op 12 juli het 'thuisfront' op de hoogte te houden van onze verrichtingen. Er moet dus een bericht via de mobiel naar de Twitter van de drumband. Je kunt een direct bericht sturen door:
d naamTwitterDrumband bericht te sturen.

Dus eerst de API van Twitter erbij.
http://apiwiki.twitter.com/Things-Every-Developer-Should-Know

Hier staan een paar simpele voorbeelden van curl aanroepen.
Nu heb ik op mijn laptop Ubuntu draaien, met Apache. Ik had al eerder een of andere plugin werkend gekregen, nu moet ik de curl aan de praat krijgen. Als ik in een php pagina het volgende zet:

<?
phpinfo();
?>

krijg je een overzicht van alle functies, instellingen en mogelijkheden van Apache. Hier staat GEEN curl tussen.

We pakken google erbij en typen deze zoekwoorden in:
ubuntu apache get curl working

Een gewone sudo apt-get install curl werkt niet, het volgende werkt beter:
apt-get install php5-curl
(gevonden op http://www.vpsmedia.com/forum/showthread.php?t=5).

Hierna een herstart van Apache:
sudo /etc/init.d/apache2 restart

In mijn phpinfo() staat nu wel netjes een onderdeel curl.
phpinfo vink ik uit, en zet de originele code weer aan.
En ik krijg nu netjes wat XML informatie terug!

<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/friends_timeline.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Even zoeken, uiteindelijk kom ik op de goeie URL:
http://apiwiki.twitter.com/Twitter-API-Documentation

Onderstaande geeft me de 'direct messages' terug:
<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Nu nog dat het niet rechtstreeks naar de browser gaat, maar naar een lokale variable
(maak ik een XML document van, dan wordt het parsen straks wat makkelijker);
Dat gaat ook makkelijk, volgende toevoegen:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var

en wijziging:
$result = curl_exec($ch);


Het resultaat bevat de laatste 20 directe berichten. Nu wil ik als ik een bepaald direct
bericht verwerkt heb alleen de berichten zien die later gestuurd zijn. Ook dat kan:
http://twitter.com/direct_messages.xml?since_id=12345

Onderstaande code is het eind van mijn testpagina, ik heb nu voldoende kennis om er netjes een Joomla-module van te maken. Het onderste, uitgevinkte, deel is voor het verwijderen van het directe bericht:

<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
//curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/friends_timeline.xml");
//curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages/new.xml");
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var
curl_setopt($ch, CURLOPT_POST, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

// grab URL and pass it to the browser
$result = curl_exec($ch);
//echo $result;

$doc = new DOMDocument();
$doc->loadXML($result);

$message_id = "";
$sender_id = "";
$message_text = "";
$message_datetime = "";
$sender_screenname = "";

if ($doc->hasChildNodes()){
$messages = $doc->getElementsByTagName('direct_message');
foreach ($messages as $message){
if ($message->hasChildNodes()){

foreach ($message->childNodes as $childNode){
if ($childNode->nodeName != "#text"){
switch ($childNode->nodeName){
case "id":
$message_id = $childNode->nodeValue;
break;
case "sender_id":
$sender_id = $childNode->nodeValue;
break;
case "text":
$message_text = $childNode->nodeValue;
break;
case "created_at":
$message_datetime = $childNode->nodeValue;
break;
case "sender_screen_name":
$sender_screenname = $childNode->nodeValue;
break;
default:
break;
}
}
}

}
}
}

if ($message_id != ""){
echo $message_id . "<HR>";
echo $sender_id . "<HR>";
echo $message_text . "<HR>";
echo $message_datetime . "<HR>";
echo $sender_screenname . "<HR>";
// do stuff (save local in database). then remove the message

}
curl_close($ch);

if ($message_id != ""){
/* $ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages/destroy/" . $message_id .".xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var
curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
*/
}
?>

ENGLISH VERSION




I am doing some experiments with Twitter. I already have my own account, my mobile phone is connected to it. So when I send a message, it is added on my own Twitter account.

I made an account for the drumband, I want to use it to add messages on our website. We go to the World Music Contest in Kerkrade on july 12th, it is a neat way to inform the people at home how everything is going. So it must be possible to send a message by phone to the Twitter of the drumband. You can send a direct message with the following syntax:
d nameTwitterDrumband MESSAGE

So, first look at the API of Twitter:
http://apiwiki.twitter.com/Things-Every-Developer-Should-Know

Here I find a couple of examples with curl. I work with Ubuntu on my laptop, running PHP and Apache. When I put the following text in a PHP page:

<?
phpinfo();
?>

I get all the settings etc. of Apache (and pc) but there is NO curl in the list.

Google is your friend. So I goto google.nl and enter these words:
ubuntu apache get curl working

A normal sudo apt-get install curl does not work, but this does:
apt-get install php5-curl
(found on http://www.vpsmedia.com/forum/showthread.php?t=5).

Let's restart Apache:
sudo /etc/init.d/apache2 restart

In phpinfo() I do get a curl part now!
I put phpinfo in the comments and enable the original code.
I run it: yes! I get XML!

<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/friends_timeline.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERUSERNAME'.':'.'TWITTERPASSWORD');

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

I need to search, but finally I get the right docs URL:
http://apiwiki.twitter.com/Twitter-API-Documentation

The below code returns my 'direct messages':
<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

I want to filter the result, so I put the result in a local variable.
Therefore we need to update the code:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var

en change:
$result = curl_exec($ch);

The result contains the last 20 direct messages. I want to retrieve only the messages I haven't processed already. That is possible:
http://twitter.com/direct_messages.xml?since_id=12345

The code below is the end of my testingpage. I have enough knowledge now to make a neat Joomla-module of it. The final, commented, part is used to delete the direct message you processed.

<?
//phpinfo();
//exit;
$ch = curl_init();

// set URL and other appropriate options
//curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/friends_timeline.xml");
//curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages/new.xml");
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var
curl_setopt($ch, CURLOPT_POST, 0);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

// grab URL and pass it to the browser
$result = curl_exec($ch);
//echo $result;

$doc = new DOMDocument();
$doc->loadXML($result);

$message_id = "";
$sender_id = "";
$message_text = "";
$message_datetime = "";
$sender_screenname = "";

if ($doc->hasChildNodes()){
$messages = $doc->getElementsByTagName('direct_message');
foreach ($messages as $message){
if ($message->hasChildNodes()){

foreach ($message->childNodes as $childNode){
if ($childNode->nodeName != "#text"){
switch ($childNode->nodeName){
case "id":
$message_id = $childNode->nodeValue;
break;
case "sender_id":
$sender_id = $childNode->nodeValue;
break;
case "text":
$message_text = $childNode->nodeValue;
break;
case "created_at":
$message_datetime = $childNode->nodeValue;
break;
case "sender_screen_name":
$sender_screenname = $childNode->nodeValue;
break;
default:
break;
}
}
}

}
}
}

if ($message_id != ""){
echo $message_id . "<HR>";
echo $sender_id . "<HR>";
echo $message_text . "<HR>";
echo $message_datetime . "<HR>";
echo $sender_screenname . "<HR>";
// do stuff (save local in database). then remove the message

}
curl_close($ch);

if ($message_id != ""){
/* $ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://twitter.com/direct_messages/destroy/" . $message_id .".xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // put data in local var
curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch , CURLOPT_USERPWD,'TWITTERGEBRUIKERSNAAM'.':'.'TWITTERWACHTWOORD');

curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
*/
}
?>

Geen opmerkingen:

Een reactie posten