Skip to content
Browse files

No commit message

  • Loading branch information...
0 parents commit 3f2c6c921896f37358fa773eb9513db9b298f300 @dg committed Jul 2, 2008
Showing with 212 additions and 0 deletions.
  1. +27 −0 license.txt
  2. +21 −0 load.php
  3. +54 −0 readme.txt
  4. +9 −0 send.php
  5. +101 −0 twitter.class.php
27 license.txt
@@ -0,0 +1,27 @@
+Copyright (c) 2008, Copyright (c) 2008 David Grudl
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of David Grudl nor the names of its
+ contributors may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 load.php
@@ -0,0 +1,21 @@
+<?php
+
+require_once 'twitter.class.php';
+
+
+$twitter = new Twitter('pokusnyucet2', '123456');
+
+$withFriends = FALSE;
+$channel = $twitter->load($withFriends);
+
+?>
+
+<h1><?$channel->title?></h1>
+
+<ul>
+<?foreach ($channel->item as $item): ?>
+ <li><?=$item->description?>
+ (<a href="<?=$item->link?>"><?=date("j.n.Y H:s", strtotime($item->pubDate))?></a>)
+ </li>
+<?endforeach?>
+</ul>
54 readme.txt
@@ -0,0 +1,54 @@
+Twitter for PHP (c) David Grudl, 2008 (http://davidgrudl.com)
+
+
+Introduction
+------------
+
+This is very small and easy library for sending messages to Twitter
+and receiving statuses.
+
+Twitter's API documentation: http://groups.google.com/group/twitter-development-talk/web/api-documentation
+My PHP blog: http://phpfashion.com
+
+
+Requirements
+------------
+- PHP (version 5 or better)
+- cURL extension
+
+
+Usage
+-----
+
+Create object using your credentials (user name and password)
+
+ $twitter = new Twitter($userName, $password);
+
+Method send() updates your status. The message must be encoded in UTF-8:
+
+ $twitter->send('I am fine today.');
+
+Method load() returns the 20 most recent statuses posted in the
+last 24 hours from you and your friends (optionally):
+
+ $withFriends = FALSE;
+ $channel = $twitter->load($withFriends);
+
+Returned channel is a SimpleXMLElement object. Extracting the
+informations from channel is easy:
+
+ echo "Title: ", $channel->title;
+
+ foreach ($channel->item as $item) {
+ echo "Message: ", $item->description;
+ echo "posted at " , $item->pubDate;
+ }
+
+
+Files
+-----
+readme.txt - This file.
+license.txt - The license for this software (New BSD License).
+twitter.class.php - The core Twitter class source.
+send.php - Example sending message to Twitter.
+load.php - Example loading statuses from Twitter.
9 send.php
@@ -0,0 +1,9 @@
+<?php
+
+require_once 'twitter.class.php';
+
+
+$twitter = new Twitter('pokusnyucet2', '123456');
+$status = $twitter->send('Mám se fajn II');
+
+echo $status ? 'OK' : 'ERROR';
101 twitter.class.php
@@ -0,0 +1,101 @@
+<?php
+
+/**
+ * Twitter for PHP
+ *
+ * @author David Grudl
+ * @copyright Copyright (c) 2008 David Grudl
+ * @license New BSD License
+ * @link http://phpfashion.com/
+ * @version 1.0
+ */
+class Twitter
+{
+ /** @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 (!function_exists('curl_version')) {
+ throw new Exception('PHP extension CURL is not loaded.');
+ }
+
+ $this->user = $user;
+ $this->pass = $pass;
+ }
+
+
+
+ /**
+ * Sends message to the Twitter.
+ * @param string message encoded in UTF-8
+ * @return boolean TRUE on success or FALSE on failure
+ */
+ public function send($message)
+ {
+ $url = 'http://twitter.com/statuses/update.xml?status=' . urlencode($message);
+ return strpos($this->httpRequest($url), '<created_at>') !== 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';
+ $url = "http://twitter.com/statuses/$line/$this->user.rss";
+ $feed = $this->httpRequest($url);
+ if ($feed === FALSE) {
+ throw new Exception('Cannot load RSS channel.');
+ }
+
+ $xml = new SimpleXMLElement($feed);
+ if (!$xml || !$xml->channel) {
+ throw new Exception('Invalid RSS channel.');
+ }
+
+ return $xml->channel;
+ }
+
+
+
+ /**
+ * Process HTTP request.
+ * @param string URL
+ * @return string|FALSE
+ */
+ private function httpRequest($url)
+ {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_USERPWD, "$this->user:$this->pass");
+ curl_setopt($ch, CURLOPT_HEADER, FALSE);
+ curl_setopt($ch, CURLOPT_POST, TRUE);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
+ $result = curl_exec($ch);
+ // debug: echo curl_errno($ch), ', ', curl_error($ch), htmlspecialchars($result);
+ $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if (curl_errno($ch) !== 0 || $code < 200 || $code >= 300) {
+ return FALSE;
+ } else {
+ return $result;
+ }
+ }
+
+}

0 comments on commit 3f2c6c9

Please sign in to comment.
Something went wrong with that request. Please try again.