Az elmúlt napokban a Twitter-rel foglalkoztunk az MNK sorozatunkban. Most elérkeztünk oda, hogy az "utánzások" után küldünk üzenetet is nekik (vagyis a követőinknek).
Kód
function postToTwitter($username,$password,$message){
$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_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);
//echo "http kód: ".$resultArray['http_code']."<br />";
if($resultArray['http_code'] == "200"){
echo "<br />OK! posted to http://twitter.com/".$username."/<br />";
} else {
echo "eek! yegads! error posting to Twitter";
}
// ellenörizzük a végeredményt
// echo "<pre>";
// print_r($resultArray);
// echo "</pre><hr>";
// $sResult = htmlentities($result);
// $sResult = str_replace("><","><br /><",$sResult);
// echo "<pre>";
// print $sResult;
// echo "</pre>";
}
Használat
postToTwitter("felhasználónév","jelszó","Ezt üzeneem a scriptel");