1. Trang chủ
  2. » Công Nghệ Thông Tin

Plug in PHP 100 POWER SOLUTIONS- P47 potx

5 203 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 301,72 KB

Nội dung

196 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 196 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s The Plug-in function PIPHP_SendTweet($user, $pass, $text) { $url = 'http://twitter.com/statuses/update.xml'; $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, "$url"); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_POST, 1); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$text"); curl_setopt($curl_handle, CURLOPT_USERPWD, "$user:$pass"); $result = curl_exec($curl_handle); curl_close($curl_handle); $xml = simplexml_load_string($result); if ($xml == FALSE) return FALSE; elseif ($xml->text == $text) return TRUE; else return FALSE; } Send Direct Tweet You can also send direct messages to other Twitter users as long as you are both following each other. This is an invaluable way to chat with other Twitter users without clogging up your public Twitter feed. Figure 8-7 shows the result of sending a direct message to the Twitter user otheruser. About the Plug-in This plug-in accepts the name and password for a Twitter account, along with the name of the Twitter user being sent the direct message and the message to Tweet. It then sends the message to that user’s account. Upon success it returns TRUE, otherwise FALSE. It takes these arguments: • $user A Twitter username • $pass The matching password for $user • $to The direct Tweet’s recipient • $text Up to 140 characters of text to Tweet FIGURE 8-7 You can also send direct Tweets to other Twitter users. 57 C h a p t e r 8 : C h a t a n d M e s s a g i n g 197 C h a p t e r 8 : C h a t a n d M e s s a g i n g 197 Variables, Arrays, and Functions $url String containing the URL of Twitter’s status update API $curl_handle Handle returned by curl_init() $result XML result of calling curl_exec() $xml XML object created from $result How It Works This plug-in is substantially similar to the previous one, with just a few minor differences. So, rather than explain its workings in full, I’ll just cover the differences. These are that an additional argument, $to, is required by the plug-in, which then gets appended to the CURLOPT_POSTFIELDS option as &user=$to, and the message now takes the different format of text=$text instead of status=$text. So the full string passed to CURLOPT_POSTFIELDS now becomes user=$to&text=$text. Also the API URL is different for sending a direct message and is now /direct_ messages/new.xml instead of /statuses/update.xml. Apart from that, it’s essentially the same code. It’s true, you can send a direct Tweet to another Twitter user by starting it with the letter d, followed by a space and then the person’s username, but the data returned by curl_ exec() would then only be that of the most recent Tweet because of using the /statuses/ update.xml API URL. However, by using the API URL of /direct_messages/new.xml, and passing the recipient to it as well as the message, Twitter will return the most recent direct message data when the Tweet is sent. You can then check this to see if the value in $test was actually posted, and whether the direct message was successfully sent. How to Use It To send a direct Tweet, ensure that both the sending and recipient Twitter accounts follow each other, and then call the plug-in, like this: $user = 'twitteruser'; $pass = 'twitterpass'; $to = 'otheruser'; $text = 'This is a direct Tweet'; $result = PIPHP_SendDirectTweet($user, $pass, $to, $text); If all is well, $result will be set to a value of TRUE, otherwise it will be FALSE upon failure. You can use this value as follows to display a success or failure message: if ($result) echo "Direct Tweet '$text' sent"; else echo "Direct Tweet '$text' failed"; The Plug-in function PIPHP_SendDirectTweet($user, $pass, $to, $text) { $text = substr($text, 0, 140); $url = 'http://twitter.com/direct_messages/new.xml'; 198 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 198 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, "$url"); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_POST, 1); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "user=$to&text=$text"); curl_setopt($curl_handle, CURLOPT_USERPWD, "$user:$pass"); $result = curl_exec($curl_handle); curl_close($curl_handle); $xml = simplexml_load_string($result); if ($xml == FALSE) return FALSE; elseif ($xml->text == $text) return TRUE; else return FALSE; } Get Tweets Here’s the last of the triumvirate of Twitter treats. It’s a plug-in to fetch up to the last 20 posts of any Twitter user who’s profile isn’t private. Figure 8-8 shows the result of pointing the plug-in at Eminem’s Twitter feed. About the Plug-in This plug-in accepts the username of a Twitter account, and as long as it’s not private, it returns the most recent Tweets. Upon success, it returns a two-element array, the first of which is the number of Tweets found, and the second is an array containing the Tweets. On failure, it returns a single-element array with the value FALSE. It takes this argument: • $user A Twitter username FIGURE 8-8 With this plug-in, you can fetch the most recent posts (up to 20) of a Twitter user. 58 C h a p t e r 8 : C h a t a n d M e s s a g i n g 199 C h a p t e r 8 : C h a t a n d M e s s a g i n g 199 Variables, Arrays, and Functions $url String containing the URL of Twitter’s user timeline API $file String containing the data returned by $url $xml XML object created from $file $tweets Array of the most recent Tweets (up to 20) from $user $tweet String containing each property of $xml->status as it is processed $timestamp Unix timestamp extracted from the date and time of a Tweet How It Works This plug-in fetches a Twitter user’s timeline feed from the following URL, where username is the name of the user: http://twitter.com/statuses/user_timeline/username.xml If the account is not set to private, then the feed is returned in XML format. This is achieved by setting $url to the URL to be retrieved and then passing it to the file_get_ contents() function, from where the XML is loaded into the string $file. If $file is zero characters in length, then a single-element array with the value FALSE is returned because the feed could not be retrieved, or the username was invalid. Otherwise, a new XML object called $xml is created from the contents of $file using the simplexml_load_string() function. If the object’s value is FALSE, then the XML was invalid or otherwise unusable so, again, an array with the value FALSE is returned. Now the plug-in is ready to extract the Tweets from a feed, so the array $tweets is initialized and then a foreach loop steps through each $xml->status property in the object, passing it into the object $tweet. From here the strtotime() function is used to convert the time stored in the property $tweet->created_at into a standard Unix timestamp value, which is then stored in $timestamp. This allows the plug-in to replace the very awkward and overly precise dates and times used by Twitter, such as Thu Jun 25 21:28:18 +0000 2009, with much more friendly and readable strings, like Jun 25th, 9:28pm. This is done using the date() function with a formatting argument of "M jS, g:ia". This new version of the date and time is then surrounded by brackets and followed by the Tweet itself, as retrieved from the property $tweet->text, and the resulting string is assigned to the next available element of the array $tweets. Once all the Tweets have been processed, a two-element array is returned, the first element of which contains the number of Tweets returned, while the second is an array containing all the Tweets. Incidentally, the @ symbols are in the code to suppress any warning error messages that might otherwise be displayed. How to Use It To use the plug-in, just call it, passing the name of a Twitter user with a public account, like this: $user = 'stephenhawking'; $result = PIPHP_GetTweets($user); 200 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 200 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s You can then test the value(s) returned by checking $result[0], like this: if (!$result[0]) echo 'Failed'; If $result[0] doesn’t contain the value FALSE, then $result[1] will contain an array of all the Tweets, which can be displayed like this: for ($j = 0 ; $j < $result[0] ; ++$j) echo $result[1][$j] . "<br />"; As with most of the plug-ins in this book, this one handles the task of manipulating the data and returning it to you in a sensible format. It’s then up to you how you choose to display the result, but the preceding code will, at the very least, provide the information you want. The Plug-in function PIPHP_GetTweets($user) { $url = "http://twitter.com/statuses/user_timeline/$user.xml"; $file = @file_get_contents($url); if (!strlen($file)) return array(FALSE); $xml = @simplexml_load_string($file); if ($xml == FALSE) return array(FALSE); $tweets = array(); foreach ($xml->status as $tweet) { $timestamp = strtotime($tweet->created_at); $tweets[] = "(" . date("M jS, g:ia", $timestamp) . ") " . $tweet->text; } return array(count($tweets), $tweets); } Replace Smileys In the early days of bulletin boards, emoticons were invented as a means of expressing emotions not quickly conveyable in brief messages. These included the familiar :) and :( happy and unhappy faces, as well as dozens more. Nowadays you still see them, but they are more often replaced with icons such as smileys. In fact, many e-mail programs and other applications such as Microsoft Word will substitute emoticons for smileys automatically for you. And that’s exactly the functionality that this plug-in offers. Figure 8-9 shows the set of 20 smileys provided by the plug-in, a few of which (such as the kiss smiley) are animated to better convey their meaning. 59 . It’s a plug- in to fetch up to the last 20 posts of any Twitter user who’s profile isn’t private. Figure 8-8 shows the result of pointing the plug- in at Eminem’s Twitter feed. About the Plug- in This. s a g i n g 199 Variables, Arrays, and Functions $url String containing the URL of Twitter’s user timeline API $file String containing the data returned by $url $xml XML object created from. array containing all the Tweets. Incidentally, the @ symbols are in the code to suppress any warning error messages that might otherwise be displayed. How to Use It To use the plug- in, just call

Ngày đăng: 07/07/2014, 08:20