ใช้งาน Twitter API ด้วย PHP ผ่าน cUrl
การใช้งาน Twitter API นั้นง่ายที่สุดแล้ว แต่ จะว่าไป ก็ ยังยากสำหรับมือใหม่อยู่ดี วันนี้เลยจะมาเสนอ twitter api บรรทัดเดียวให้ดูกันว่า twitter API นั้นใช้งานง่ายจิงๆ
ก่อนอื่นเลย สิ่งที่จะต้องใช้ในงานนี้คือ
- ภาษาเขียนโปรแกรมอะไรก็ได้นะที่ execute command ได้
- ความรู้ในการต่อ string
- สามารถใช้ cUrl ได้
งั้นเรามาเขียนกันเลย
การเขียนนั้นง่ายแสนง่ายเพราะ cUrl นั่้นง่ายนิดเดียว เราจะมีหลักการเขียนที่ว่า
curl –basic –user USERNAME:PASS –data status=”MESSAGE” http://twitter.com/statuses/update.xml
ง่ายๆแค่นี้เอง
แล้วเราก็ทำ form ให้ส่งค่าข้อมูลไป หรือ จะเอาข้อมูลมาจากใหนก็ได้ โดยจะแสดงตัวอย่างเป็น PHP นะง่ายดี
<?php
if($_POST['message'] != ”){
$message = $_POST['message'];
$username = $_POST['username'];
$password = $_POST['password'];$command = ‘curl –basic –user ‘.$username.’:’.$password.’ –data status=”‘.$message.’” http://twitter.com/statuses/update.xml’;
$retval = system($command);
echo $command;
echo ‘<br />’;
echo $retval;}
?>
<form name=”twitterForm” action=”" method=”post” enctype=”multipart/form-data”>
message. <input type=”text” name=”message” value=”" /><br />
username. <input type=”text” name=”username” value=”" /><br />
password. <input type=”text” name=”password” value=”" /><br /><input type=”submit” value=”OK” />
</form>
อย่างนี้เป็นต้นนะครับผม
ลองเล่นๆกันดู post เข้าไปเลย twitter อ่ะ มันส์ ช่วย SEO ได้ด้วยนะ
ปล. web hosting ส่วนใหญ่ใช้ apache และจะ config saft_mode เป็น on จะไม่สามารถใช้ system(), exec(), shell_exec() ได้ ให้ดูด้วยนะว่าใช้ได้เปล่า