1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id$ 30 */ 31 32 namespace Scrivo\SocialMedia; 33 34 /** 35 * Get a number of tweets from a user timeline. 36 * 37 * Example (using bogus values): 38 * 39 * $tut = new TwitterUserTimeline( 40 * "xvz1evFS4wEEPTGEFPHBog", //< consumer key 41 * "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", //< consumer secret 42 * "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", //< access token 43 * "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE" //< access token secret 44 * ); 45 * 46 * echo $tut->getTweets(array("screen_name"=>"yardinternet", "count"=>20)); 47 */ 48 class TwitterUserTimeline extends OAuth { 49 50 /** 51 * Get OAuth autorized data. 52 * @param string $url The URL of the service. 53 * @param array $param The request parameters given as an array. 54 * @return string The response data. 55 * @throws Exception If there was a problem with retrieving data from 56 * the $url. 57 */ 58 private function getData($url, array $param) { 59 60 $od = $this->getAuthorizationData("get", $url, $param); 61 62 $rr = new RawRequest($od->scheme, $od->hostname); 63 64 $result = $rr->getResponse( 65 "GET {$od->baseUrl}?{$od->parameterString} HTTP/1.1\r\n". 66 "Accept: */*\r\n". 67 "Connection: close\r\n". 68 "User-Agent: Scrivo PHP OAuth\r\n". 69 $od->authorisationHeader. 70 "Host: {$od->hostname}\r\n". 71 "\r\n"); 72 73 return $result->data; 74 } 75 76 /** 77 * Get tweets as a HTML unordered list. 78 * @param array $param The request parameters given as an array for the 79 * Twitter url "https://api.twitter.com/1.1/statuses/user_timeline.json". 80 * @return string HTML data containing the tweets. 81 */ 82 public function getTweets(array $param) { 83 84 try { 85 $twitter = json_decode($this->getData( 86 "https://api.twitter.com/1.1/statuses/user_timeline.json", 87 $param)); 88 } catch (Excepetion $e) { 89 return "<ul class='tweets'></ul>"; 90 } 91 92 $res = "<ul class='tweets'>"; 93 foreach($twitter as $tweet) { 94 $replace = array(); 95 $with = array(); 96 foreach ($tweet->entities->hashtags as $h) { 97 $replace[] = "#{$h->text}"; 98 $with[] = "<a href='http://www.twitter.com/search?q=%23". 99 "{$h->text}&src=hash' target='_blank'>#{$h->text}</a>"; 100 } 101 foreach ($tweet->entities->symbols as $symbol) { 102 // Is this the bleeding edge of technology? 103 } 104 foreach ($tweet->entities->urls as $url) { 105 $replace[] = $url->url; 106 $with[] = "<a href='{$url->expanded_url}' target='_blank'>" 107 ."{$url->display_url}</a>"; 108 } 109 foreach ($tweet->entities->user_mentions as $um) { 110 $replace[] = "@{$um->screen_name}"; 111 $with[] = "<a href='http://www.twitter.com/{$um->screen_name}'" 112 ." target='_blank'>@{$um->screen_name}</a>"; 113 } 114 $res .= "<li>".str_replace($replace, $with, $tweet->text)."</li>"; 115 } 116 $res .= "</ul>"; 117 return $res; 118 } 119 } 120 121 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013