Venez baraguiner avec la communauté !
Vous n'êtes pas identifié.
Je donne une de mes application pour twitter qui sert à envoyer des messages et recevoir le dernier tweets posté.
<?php
// Pour pouvoir remplir les deux premières constantes, il faut se connecter sur le site shr.im depuis twitter : http://shr.im/login/
define('API_USER', 'bourdais6');
define('API_KEY', 'XXXXXXX');
define('TWITTER_USER', 'bourdais6');
define('TWITTER_PASS', 'XXXXXXXX');
function shr_im($url) {
$url = $url[0];
$host = 'http://shr.im/api/1.0/post.json?api_user='.API_USER.'&api_key='.API_KEY.'&url_src='.$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(curl_exec($ch), true);
return $result[0]['alias'];
}
function postToTwitter($username,$password,$message) {
$message = preg_replace_callback('#(http://[a-zA-Z0-9_\-.]+)#is', 'shr_im', $message);
// Fonction pompée sur le net
$host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == "200") {
$twitter_status='Message envoyé ! <a href="http://twitter.com/'.$username.'">Voir sur le profil</a>
<br /> Votre dernier tweet: '.last_twitte("bourdais6").'.; }
else $twitter_status = "Erreur, requete refusée (".$resultArray['http_code'].").";
return $twitter_status;
}
?>
<?php
// La fonction.
function last_twitte($pseudo,$fichier_xml="cache.xml",$time_diference=60){
if (file_exists($fichier_xml)){
if(time()-fileatime($fichier_xml)>$time_diference){
$twitter = curl_init();
curl_setopt($twitter, CURLOPT_URL, "http://www.twitter.com/statuses/user_timeline/".$pseudo.".xml?count=1");
curl_setopt($twitter, CURLOPT_TIMEOUT,2);
curl_setopt($twitter, CURLOPT_RETURNTRANSFER,true);
$contenue = curl_exec($twitter);
if($contenue==false){
$contenue = file_get_contents($fichier_xml);
}
else {
file_put_contents($fichier_xml,$contenue);
}
}else{
$contenue = file_get_contents($fichier_xml);
}
}else{
$twitter = curl_init();
curl_setopt($twitter, CURLOPT_URL, "http://www.twitter.com/statuses/user_timeline/".$pseudo.".xml?count=1");
curl_setopt($twitter, CURLOPT_TIMEOUT,2);
curl_setopt($twitter, CURLOPT_RETURNTRANSFER,true);
$contenue = curl_exec($twitter);
file_put_contents($fichier_xml,$contenue);
}
$xml = new SimpleXMLElement($contenue);
return $xml->status[0]->text;
}
?>Voilà pour la fonctions...
et pour la page pour afficher tous sa(inclure le fichier fonctions.php dener ci-dessus):
<?php
require_once('fonctions.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//FR" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Mes derniers Tweet</title>
</head>
<body>
<h1>Mes derniers Tweet</h1>
<?php
$c = last_twitte("bourdais6");
$content = '<bold><MARQUEE>'.$c.'</MARQUEE></bold>';
define('TWEET', $content);
echo TWEET;
?>
<hr />
<h1>Envoyez un tweet</h1>
<form action="?" method="post">
<center>
<label for="msg"> Votre message:<input type="text" name="msg" /></label><br />
<input type="submit" value="Envoyer" />
</center>
</form>
<?php
if($_POST['msg']){
$msg = $_POST['msg'];
echo postToTwitter(TWITTER_USER, TWITTER_PASS, $msg);
}
?>
</body>
</html>Cela reste vraiment basique et un bug persiste. Je vous laisse le découvrir!

Hors ligne
Bonjour,
La méthode avec Curl ne fonctionne plus depuis le 1er septembre 2010 car Twitter a désactivé l'authentification basique.
Il faut maintenant utiliser une méthode plus complexe : OAuth.
Voici un tuto simple expliquant une manière de faire :
http://yanncochard.com/tuto/envoyer-un- … ur-twitter
Hors ligne