PDA

View Full Version : twitter tự update tin từ mysite



KuJangSung
28-10-2009, 17:13
làm sao để twitter của mình tự update tin của site mình đc nhi? :D

yourvietnam
29-10-2009, 11:26
<?php
/**
* Twitter for PHP - library for sending messages to Twitter and receiving status updates.
*
* @author David Grudl
* @copyright Copyright (c) 2008 David Grudl
* @license New BSD License
* @link http://phpfashion.com/
* @version 1.0
*/
class Twitter
{
/** @var int */
public static $cacheExpire = 1800; // 30 min
/** @var string */
public static $cacheDir;
/** @var user name */
private $user;
/** @var password */
private $pass;

/**
* Creates object using your credentials.
* @param string user name
* @param string password
* @throws Exception
*/
public function __construct($user, $pass)
{
if (!extension_loaded('curl')) {
throw new Exception('PHP extension CURL is not loaded.');
}

$this->user = $user;
$this->pass = $pass;
}
/**
* Tests if user credentials are valid.
* @return boolean
* @throws Exception
*/
public function authenticate()
{
$xml = $this->httpRequest('http://twitter.com/account/verify_credentials.xml');
return (bool) $xml;
}
/**
* Sends message to the Twitter.
* @param string message encoded in UTF-8
* @return mixed ID on success or FALSE on failure
*/
public function send($message)
{
$xml = $this->httpRequest(
'https://twitter.com/statuses/update.xml',
array('status' => $message)
);
return $xml && $xml->id ? (string) $xml->id : FALSE;
}
/**
* Returns the 20 most recent statuses posted from you and your friends (optionally).
* @param bool with friends?
* @return SimpleXMLElement
* @throws Exception
*/
public function load($withFriends)
{
$line = $withFriends ? 'friends_timeline' : 'user_timeline';
$xml = $this->httpRequest("http://twitter.com/statuses/$line/$this->user.xml", FALSE);
if (!$xml || !$xml->status) {
throw new Exception('Cannot load channel.');
}
return $xml;
}
/**
* Process HTTP request.
* @param string URL
* @param array of post data (or FALSE = cached get)
* @return SimpleXMLElement|FALSE
*/
private function httpRequest($url, $post = NULL)
{
if ($post === FALSE && self::$cacheDir) {
$cacheFile = self::$cacheDir . '/twitter.' . md5($url) . '.xml';
if (@filemtime($cacheFile) + self::$cacheExpire > time()) {
return new SimpleXMLElement(@file_get_contents($cacheFile));
}
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERPWD, "$this->user:$this->pass");
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));

if ($post) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
$result = curl_exec($curl);
$ok = curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200; // code 200 is required

if (!$ok) {
if (isset($cacheFile)) {
$result = @file_get_contents($cacheFile);
if (is_string($result)) {
return new SimpleXMLElement($result);
}
}
return FALSE;
}

if (isset($cacheFile)) {
file_put_contents($cacheFile, $result);
}

return new SimpleXMLElement($result);
}
}
?>
-------------
Save cái đoạn php trên thành 1 file twitter.class.php để include vào trang sử dụng.
Khi nào dùng thì gọi hàm send và truyền nội dung vào là xong:
------------

$twitter = new Twitter('username', 'pass');
$status = $twitter->send($message);

KuJangSung
29-10-2009, 11:44
em ko rành php nên chưa rõ lắm, cụ thể là muốn cập nhật các bài viết của diễn đàn qua twitter thì như thế nào nhỉ? như twitter/ddth vậy :D

dungsg
29-10-2009, 14:46
Dùng cái này nè bạn http://twitterfeed.com/ update từ rss lên twitter

KuJangSung
29-10-2009, 18:42
cám ơn các bạn nhiều :D

vietravel247.com
30-10-2009, 21:13
Trên diễn đàn này khi bạn post tin mới tự động cập nhật lên twitter : www.vietravel247.com/forumvn/

ectrade351
12-08-2010, 01:41
Need a steady supply of below wholesale, Name-Brand Fashion Apparel and Clothing? TradeAllStar.com Marketplace liquidates $3 million worth of retail inventory daily. Deeply Discounted Closeouts and Customer Returned Lots of Women's and Men's Apparel & Clothing (http://www.tradeallstar.com/Wholesale-hot-brand-sales_c871): Shirts (http://www.tradeallstar.com/Wholesale-af-dress-and-skirt_c886), Tops, Dresses (http://www.tradeallstar.com/Wholesale-ed-hardy-mens-swim-trunks_c911), Polo's, Dress Pants, Men's Clothing (http://www.tradeallstar.com/Wholesale-ed-hardy-mens-tshirts_c904), Plus Sizes, Pants, Shoes (http://www.tradeallstar.com/Wholesale-ed-hardy-boots_c921), Footwear and Fashion Accessories Direct from America’s largest retailers.

bep360
11-10-2010, 23:09
Em bị ban 3 cái nick trên twitter rồi. Hixx