Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added current code to the repository.

  • Loading branch information...
commit 2a9c6086b52a880b6d4a35f05e7b958cbc166415 0 parents
jstump authored
Showing with 1,706 additions and 0 deletions.
  1. +170 −0 Services/Twitter.php
  2. +70 −0 Services/Twitter/Account.php
  3. +213 −0 Services/Twitter/Common.php
  4. +152 −0 Services/Twitter/DirectMessages.php
  5. +107 −0 Services/Twitter/Exception.php
  6. +60 −0 Services/Twitter/Favorites.php
  7. +92 −0 Services/Twitter/Friendships.php
  8. +60 −0 Services/Twitter/Notifications.php
  9. +207 −0 Services/Twitter/Statuses.php
  10. +94 −0 Services/Twitter/Users.php
  11. +24 −0 tests/005-services-twitter-statuses-public-timeline.phpt
  12. +24 −0 tests/006-services-twitter-statuses-friends-timeline.phpt
  13. +24 −0 tests/007-services-twitter-statuses-user-timeline.phpt
  14. +15 −0 tests/010-services-twitter-statuses-show.phpt
  15. +20 −0 tests/015-services-twitter-statuses-replies.phpt
  16. +23 −0 tests/020-services-twitter-statuses-update.phpt
  17. +28 −0 tests/025-services-twitter-statuses-destroy.phpt
  18. +39 −0 tests/026-services-twitter-statuses-friends.phpt
  19. +30 −0 tests/027-services-twitter-statuses-followers.phpt
  20. +30 −0 tests/028-services-twitter-statuses-featured.phpt
  21. +19 −0 tests/030-services-twitter-exception.phpt
  22. +23 −0 tests/100-services-twitter-users-show.phpt
  23. +39 −0 tests/200-services-twitter-direct-messages.phpt
  24. +24 −0 tests/205-services-twitter-direct-messages-sent.phpt
  25. +16 −0 tests/210-services-twitter-direct-messages-new.phpt
  26. +24 −0 tests/215-services-twitter-direct-messages-destroy.phpt
  27. +16 −0 tests/300-services-twitter-friendships-create.phpt
  28. +16 −0 tests/305-services-twitter-friendships-destroy.phpt
  29. +15 −0 tests/400-services-twitter-account-verify-credentials.phpt
  30. +32 −0 tests/tests-config.php.example
170 Services/Twitter.php
@@ -0,0 +1,170 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+require_once 'Services/Twitter/Exception.php';
+require_once 'Services/Twitter/Common.php';
+
+/**
+ * Services_Twitter
+ *
+ * <code>
+ * <?php
+ * require_once 'Services/Twitter.php';
+ *
+ * $username = 'You_Username';
+ * $password = 'Your_Password';
+ *
+ * try {
+ * $twitter = new Services_TinyURL($username, $password);
+ * $msg = $twitter->statuses->update("I'm coding with PEAR right now!");
+ * print_r($msg); // Should be a SimpleXMLElement structure
+ * } catch (Services_Twitter_Exception $e) {
+ * echo $e->getMessage();
+ * }
+ * ?>
+ * </code>
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter extends Services_Twitter_Common
+{
+ /**
+ * Twitter API error codes
+ *
+ * @global integer ERROR_UNKNOWN An unknown error occurred
+ * @global integer ERROR_REQUEST Bad request sent
+ * @global integer ERROR_AUTH Not authorized to do action
+ * @global integer ERROR_FORBIDDEN Forbidden from doing action
+ * @global integer ERROR_NOT_FOUND Item requested not found
+ * @global integer ERROR_INTERNAL Internal Twitter error
+ * @global integer ERROR_DOWN Twitter is down
+ * @global integer ERROR_UNAVAILABLE API is overloaded
+ */
+ const ERROR_UNKNOWN = 1;
+ const ERROR_REQUEST = 400;
+ const ERROR_AUTH = 401;
+ const ERROR_FORBIDDEN = 403;
+ const ERROR_NOT_FOUND = 404;
+ const ERROR_INTERNAL = 500;
+ const ERROR_DOWN = 502;
+ const ERROR_UNAVAILABLE = 503;
+
+ /**
+ * Public URI of Twitter's API
+ *
+ * @var string $uri URI of Twitter API
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ static public $uri = 'http://twitter.com';
+
+ /**
+ * Supported areas / methods of Twitter's API
+ *
+ * @var array $methods
+ * @see Services_Twitter::__get()
+ */
+ static protected $methods = array(
+ 'statuses' => 'Statuses',
+ 'users' => 'Users',
+ 'direct_messages' => 'DirectMessages',
+ 'friendships' => 'Friendships',
+ 'account' => 'Account'
+ );
+
+ /**
+ * Instances of Twitter methods
+ *
+ * @var array $instances
+ * @see Services_Twitter::__get(), Services_Twitter::$methods
+ */
+ static protected $instances = array();
+
+ /**
+ * Lazily load Twitter API drivers
+ *
+ * @param string $var Method to load
+ *
+ * @throws Services_Twitter_Exception
+ * @return object Instance of API driver
+ * @see Services_Twitter::factory()
+ */
+ public function __get($var)
+ {
+ if (!isset(self::$methods[$var])) {
+ throw new Services_Twitter_Exception(
+ 'Method (' . $var . ') is not implemented'
+ );
+ }
+
+ return $this->factory(self::$methods[$var]);
+ }
+
+ /**
+ * Instantiate API driver
+ *
+ * @param string $method API driver to load
+ *
+ * @return object Instance of API driver
+ */
+ protected function factory($method)
+ {
+ if (isset(self::$instances[$method])) {
+ return self::$instances[$method];
+ }
+
+ $file = 'Services/Twitter/' . $method . '.php';
+ include_once $file;
+
+ $class = 'Services_Twitter_' . $method;
+ self::$instances[$method] = new $class($this->user, $this->pass);
+ return self::$instances[$method];
+ }
+}
+
+?>
70 Services/Twitter/Account.php
@@ -0,0 +1,70 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Account
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Account extends Services_Twitter_Common
+{
+ /**
+ * Verify a user's credentials
+ *
+ * @return boolean
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function verify_credentials()
+ {
+ $res = $this->sendRequest('/account/verify_credentials');
+ return ((string)$res === 'true');
+ }
+}
+
+?>
213 Services/Twitter/Common.php
@@ -0,0 +1,213 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+require_once 'Services/Twitter/Exception.php';
+
+/**
+ * Services_Twitter_Common
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+abstract class Services_Twitter_Common
+{
+ /**
+ * Name of call group
+ *
+ * @var string $name Used to overload class name for groupings
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ protected $name = null;
+
+ /**
+ * Username of Twitter user
+ *
+ * @var string $user Twitter username
+ */
+ protected $user = '';
+
+ /**
+ * Password of Twitter user
+ *
+ * @var string $pass User's password for Twitter
+ */
+ protected $pass = '';
+
+ /**
+ * Constructor
+ *
+ * @param string $user Twitter username
+ * @param string $pass Twitter password
+ *
+ * @return void
+ */
+ public function __construct($user, $pass)
+ {
+ $this->user = $user;
+ $this->pass = $pass;
+ }
+
+ /**
+ * Send a request to the Twitter API
+ *
+ * @param string $endPoint The API endpoint WITHOUT the extension
+ * @param array $params The API endpoint arguments to pass
+ * @param string $method Whether to use GET or POST
+ *
+ * @throws Services_Twitter_Exception
+ * @return object Instance of SimpleXMLElement
+ */
+ protected function sendRequest($endPoint,
+ array $params = array(),
+ $method = 'GET')
+ {
+ $uri = Services_Twitter::$uri . $endPoint . '.xml';
+
+ if ($method != 'GET' && $method != 'POST') {
+ throw new Services_Twitter_Exception('Unsupported method: ' . $method);
+ }
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_USERAGENT, 'Services_Twitter');
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->pass);
+
+ $sets = array();
+ foreach ($params as $key => $val) {
+ $sets[] = $key . '=' . urlencode($val);
+ }
+
+ if ($method == 'POST') {
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $sets));
+ } else {
+ if (count($sets)) {
+ $uri .= '?' . implode('&', $sets);
+ }
+ }
+
+ curl_setopt($ch, CURLOPT_URL, $uri);
+ $res = trim(curl_exec($ch));
+
+ $err = curl_errno($ch);
+ if ($err !== CURLE_OK) {
+ throw new Services_Twitter_Exception(curl_error($ch), $err, $uri);
+ }
+
+ $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if (substr($code, 0, 1) != '2') {
+ $xml = @simplexml_load_string($res);
+ if ($xml instanceof SimpleXMLElement && isset($xml->error)) {
+ throw new Services_Twitter_Exception(
+ (string)$xml->error, Services_Twitter::ERROR_UNKNOWN, $uri
+ );
+ }
+
+ throw new Services_Twitter_Exception(
+ 'Unexpected HTTP status returned from API', $code, $uri
+ );
+ }
+
+ curl_close($ch);
+
+ if (!strlen($res)) {
+ throw new Services_Twitter_Exception(
+ 'Empty response was received from the API',
+ Services_Twitter::ERROR_UNKNOWN, $uri
+ );
+ }
+
+ $xml = @simplexml_load_string($res);
+ if (!$xml instanceof SimpleXMLElement) {
+ throw new Services_Twitter_Exception(
+ 'Could not parse response received by the API',
+ Services_Twitter::ERROR_UNKNOWN, $uri
+ );
+ }
+
+ return $xml;
+ }
+
+ /**
+ * Overloaded call for API passthrough
+ *
+ * Takes the function called and magically creates an API endpoint based
+ * on the class name / grouping name and the function name. For instance,
+ * a call to $twitter->statuses->followers() would call the API endpoint
+ * '/statuses/followers.xml' and return the results.
+ *
+ * @param string $function API endpoint being called
+ * @param array $args $args[0] is an array of GET/POST arguments
+ *
+ * @return object Instance of SimpleXMLElement
+ * @see Services_Twitter_Common::sendRequest()
+ * @see Services_Twitter_Common::$name
+ */
+ public function __call($function, array $args = array())
+ {
+ if (isset($args[0]) && is_array($args[0]) && count($args[0])) {
+ $params = $args[0];
+ } else {
+ $params = array();
+ }
+
+ if (!is_null($this->name)) {
+ $endPoint = '/' . $this->name . '/' . $function;
+ } elseif (get_class($this) != 'Services_Twitter') {
+ $name = strtolower(array_pop(explode('_', get_class($this))));
+ $endPoint = '/' . $name . '/' . $function;
+ } else {
+ $endPoint = '/' . $function;
+ }
+
+ return $this->sendRequest($endPoint, $params);
+ }
+}
+
+?>
152 Services/Twitter/DirectMessages.php
@@ -0,0 +1,152 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_DirectMessages
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_DirectMessages extends Services_Twitter_Common
+{
+ /**
+ * Group name
+ *
+ * @var string $name Overload name of API group
+ * @see Services_Twitter_Common::$name
+ */
+ protected $name = 'direct_messages';
+
+ /**
+ * Delete/Destroy a direct message
+ *
+ * @param integer $id The direct message id to delete
+ *
+ * @return object Instance of SimpleXMLElement
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function destroy($id)
+ {
+ if (!is_numeric($id)) {
+ throw new Services_Twitter_Exception(
+ $id . ' is not a valid numeric id'
+ );
+ }
+
+ return $this->sendRequest('/direct_messages/destroy/' . $id);
+ }
+
+ /**
+ * __call
+ *
+ * Evidently, PHP considers 'new' a reserved keyword. I really, really
+ * wanted to keep the interface the same across the board and Twitter uses
+ * /direct_messages/new so Services_Twitter should use
+ * $foo->direct_messages->new(), which was causing parse errors. This is
+ * an ugly hack to work around this issue.
+ *
+ * @param string $function The API endpoint to call
+ * @param array $args The arguments for the API endpoint
+ *
+ * @throws Services_Twitter_Exception
+ * @return object Instance of SimpleXMLElement
+ * @see Services_Twitter_DirectMessages::dmNew()
+ * @see Services_Twitter_Common::__call()
+ */
+ public function __call($function, array $args = array())
+ {
+ switch ($function) {
+ case 'new':
+ if (!isset($args[0]) || !isset($args[1])) {
+ throw new Services_Twitter_Exception(
+ 'id and text are required when sending a direct message',
+ Services_Twitter::ERROR_UNKNOWN
+ );
+ }
+
+ return $this->dmNew($args[0], $args[1]);
+ default:
+ return parent::__call($function, $args);
+ }
+ }
+
+ /**
+ * Send a direct message
+ *
+ * @param string $user The id or username to send to
+ * @param string $text The direct text to send
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ protected function dmNew($user, $text)
+ {
+ if (preg_match('/[^a-z0-9_]/i', $user)) {
+ throw new Services_Twitter_Exception(
+ 'The user (' . $user . ') provided appears to be invalid',
+ Services_Twitter::ERROR_UNKNOWN
+ );
+ }
+
+ if (!strlen($text)) {
+ throw new Services_Twitter_Exception(
+ 'Statuses cannot be empty strings',
+ Services_Twitter::ERROR_UNKNOWN
+ );
+ }
+
+ return $this->sendRequest('/direct_messages/new', array(
+ 'user' => $user,
+ 'text' => $text
+ ), 'POST');
+ }
+}
+
+?>
107 Services/Twitter/Exception.php
@@ -0,0 +1,107 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+require_once 'PEAR/Exception.php';
+
+/**
+ * Services_Twitter_Exception
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Exception extends PEAR_Exception
+{
+ /**
+ * Call to the API that created the error
+ *
+ * @var string $call
+ */
+ protected $call = '';
+
+ /**
+ * Constructor
+ *
+ * @param string $message Error message
+ * @param integer $code Error code
+ * @param string $call API call that generated error
+ *
+ * @see Services_Twitter_Exception::$call
+ * @link http://php.net/exceptions
+ */
+ public function __construct($message = null, $code = 0, $call = '')
+ {
+ parent::__construct($message, $code);
+ $this->call = $call;
+ }
+
+ /**
+ * Return API call
+ *
+ * @return string
+ * @see Services_Twitter_Exception::$call
+ */
+ public function getCall()
+ {
+ return $this->call;
+ }
+
+ /**
+ * __toString
+ *
+ * Overload PEAR_Exception's horrible __toString implementation.
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return $this->message . ' (Code: ' . $this->code . ', Call: ' .
+ $this->call . ')';
+ }
+}
+
+?>
60 Services/Twitter/Favorites.php
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Favorites
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Favorites extends Services_Twitter_Common
+{
+
+}
+
+?>
92 Services/Twitter/Friendships.php
@@ -0,0 +1,92 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Friendships
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Friendships extends Services_Twitter_Common
+{
+ /**
+ * __call
+ *
+ * Implements:
+ * 1. Services_Twitter_Friendships::create()
+ * 2. Services_Twitter_Friendships::destroy()
+ *
+ * @param string $function Name of function being called (create/destroy)
+ * @param array $args The arguments passed to the function call
+ *
+ * @return object Instance of SimpleXMLElement
+ * @throws Services_Twitter_Exception
+ */
+ public function __call($function, array $args = array())
+ {
+ switch ($function) {
+ case 'create':
+ case 'destroy':
+ if (!isset($args[0]) ||
+ preg_match('/[^a-z0-9_]+/i', $args[0])) {
+ throw new Services_Twitter_Exception(
+ 'A valid username or user id is required'
+ );
+ }
+
+ return $this->sendRequest('/friendships/' . $function . '/' . $args[0]);
+ break;
+ default:
+ throw new Services_Twitter_Exception(
+ get_class($this) . '::' . $function . '() is not a supported method'
+ );
+ }
+ }
+}
+
+?>
60 Services/Twitter/Notifications.php
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Exception
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Notifications extends Services_Twitter_Common
+{
+
+}
+
+?>
207 Services/Twitter/Statuses.php
@@ -0,0 +1,207 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Exception
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Statuses extends Services_Twitter_Common
+{
+ /**
+ * Fetch a specific status message
+ *
+ * @param integer $id The unique numeric status ID
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function show($id)
+ {
+ return $this->sendRequest('/statuses/show/' . (int)$id);
+ }
+
+ /**
+ * Destroy a specific status message
+ *
+ * @param integer $id The unique numeric status ID
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function destroy($id)
+ {
+ return $this->sendRequest('/statuses/destroy/' . (int)$id);
+ }
+
+ /**
+ * Update the Twitter status
+ *
+ * @param string $status New Twitter status
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function update($status)
+ {
+ if (!strlen($status)) {
+ throw new Services_Twitter_Exception(
+ 'Statuses cannot be empty strings'
+ );
+ }
+
+ return $this->sendRequest('/statuses/update', array(
+ 'status' => $status
+ ), 'POST');
+ }
+
+ /**
+ * Get the user's friends' timeline
+ *
+ * This method was overridden from the normal
+ * Services_Twitter_Common::__call() because it can optionally take an $id
+ * of another user. If the key 'id' exists in the $params array then it
+ * alters the behavior of what's returned. Also, any argument other than
+ * 'id', 'since' and 'page' are currently ignored.
+ *
+ * @param array $params Parameters array
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function friends_timeline(array $params = array())
+ {
+ $allowed = array('id', 'since', 'page');
+ $tmp = array();
+ foreach ($params as $key => $val) {
+ if (in_array($key, $allowed)) {
+ $tmp[$key] = $val;
+ }
+ }
+
+ $endPoint = '/statuses/friends_timeline';
+ if (isset($tmp['id'])) {
+ $endPoint .= '/' . $tmp['id'];
+ unset($tmp['id']);
+ }
+
+ $res = $this->sendRequest($endPoint, $tmp);
+ if (!isset($res->status) ||
+ (is_array($res->status) && !count($res->status))) {
+ throw new Services_Twitter_Exception(
+ $this->user . "'s friends have no status updates or " .
+ $this->user . " has no friends"
+ );
+ }
+
+ return $res;
+ }
+
+ /**
+ * Returns up to 100 of the user's friends
+ *
+ * @param array $params Parameters array
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function friends(array $params = array())
+ {
+ $allowed = array('id', 'lite', 'page');
+ $tmp = array();
+ foreach ($params as $key => $val) {
+ if (in_array($key, $allowed)) {
+ $tmp[$key] = $val;
+ }
+ }
+
+ $tmp['lite'] = (isset($tmp['lite']) &&
+ $tmp['lite'] === true) ? 'true' : 'false';
+
+ $endPoint = '/statuses/friends';
+ if (isset($tmp['id']) && strlen($tmp['id'])) {
+ $endPoint .= '/' . $tmp['id'];
+ unset($tmp['id']);
+ }
+
+ return $this->sendRequest($endPoint, $tmp);
+ }
+
+ /**
+ * Returns up to 100 of the user's followers
+ *
+ * @param array $params Parameters array
+ *
+ * @return object Instance of SimpleXMLElement of new status
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function followers(array $params = array())
+ {
+ $allowed = array('lite', 'page');
+ $tmp = array();
+ foreach ($params as $key => $val) {
+ if (in_array($key, $allowed)) {
+ $tmp[$key] = $val;
+ }
+ }
+
+ $tmp['lite'] = (isset($tmp['lite']) &&
+ $tmp['lite'] === true) ? 'true' : 'false';
+
+ return $this->sendRequest('/statuses/followers', $tmp);
+ }
+}
+
+?>
94 Services/Twitter/Users.php
@@ -0,0 +1,94 @@
+<?php
+
+/**
+ * An interface for Twitter's HTTP API
+ *
+ * PHP version 5.1.0+
+ *
+ * Copyright (c) 2007, The PEAR Group
+ *
+ * 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 the The PEAR Group 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.
+ *
+ * @category Services
+ * @package Services_TinyURL
+ * @author Joe Stump <joe@joestump.net>
+ * @copyright 1997-2007 Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://twitter.com/help/api
+ * @link http://twitter.com
+ */
+
+/**
+ * Services_Twitter_Exception
+ *
+ * @category Services
+ * @package Services_Twitter
+ * @author Joe Stump <joe@joestump.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://twitter.com
+ */
+class Services_Twitter_Users extends Services_Twitter_Common
+{
+ /**
+ * Show extended information for a given user
+ *
+ * This will return the extended information of a given user. This
+ * information includes design settings, so third party developers can
+ * theme their widgets according to a given user's preferences.
+ *
+ * The parameter passed can be the user ID (integer) of the user, their
+ * username (string) or the user's email address (string). NOTE: Strict
+ * validation on email addresses is NOT done in this package.
+ *
+ * @param mixed $id_or_email Integer user ID, username or email address
+ *
+ * @return object Instance of SimpleXMLElement
+ * @throws Services_Twitter_Exception
+ * @see Services_Twitter_Common::sendRequest()
+ */
+ public function show($id_or_email)
+ {
+ $params = array();
+
+ /**
+ * This is by far not the most robust email checker here, but the
+ * API will return an error if it's invalid and I don't want to base
+ * this package on Validate, which isn't PHP5.
+ */
+ if (strpos($id_or_email, '@')) {
+ $endPoint = '/users/show';
+ $params['email'] = $id_or_email;
+ } else {
+ $endPoint = '/users/show/' . $id_or_email;
+ }
+
+ return $this->sendRequest($endPoint, $params);
+ }
+}
+
+?>
24 tests/005-services-twitter-statuses-public-timeline.phpt
@@ -0,0 +1,24 @@
+--TEST--
+Services_Twitter_Statuses::public_timeline()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->public_timeline();
+
+if (!count($res->status)) {
+ echo "No statuses in the public timeline!\n";
+ exit;
+}
+
+foreach ($res->status as $status) {
+ if (!is_numeric((int)$status->id)) {
+ print_r($status);
+ }
+}
+
+?>
+--EXPECT--
24 tests/006-services-twitter-statuses-friends-timeline.phpt
@@ -0,0 +1,24 @@
+--TEST--
+Services_Twitter_Statuses::friends_timeline()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->friends_timeline();
+
+if (!count($res->status)) {
+ echo "No statuses in the public timeline!\n";
+ exit;
+}
+
+foreach ($res->status as $status) {
+ if (!is_numeric((int)$status->id)) {
+ print_r($status);
+ }
+}
+
+?>
+--EXPECT--
24 tests/007-services-twitter-statuses-user-timeline.phpt
@@ -0,0 +1,24 @@
+--TEST--
+Services_Twitter_Statuses::public_timeline()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->user_timeline();
+
+if (!count($res->status)) {
+ echo "No statuses in the public timeline!\n";
+ exit;
+}
+
+foreach ($res->status as $status) {
+ if (!is_numeric((int)$status->id)) {
+ print_r($status);
+ }
+}
+
+?>
+--EXPECT--
15 tests/010-services-twitter-statuses-show.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Services_Twitter_Statuses::show()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->show(777808453);
+echo $res->text . "\n";
+
+?>
+--EXPECT--
+Mental Note: Starbucks is on the way to the bus to work.
20 tests/015-services-twitter-statuses-replies.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Services_Twitter_Statuses::replies()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->replies(777808453);
+
+foreach ($res->status as $status) {
+ if (!preg_match('/@' . $user . '/i', (string)$status->text)) {
+ echo 'Does not contain @' . $user . ':' . "\n";
+ echo (string)$status->text . "\n";
+ }
+}
+
+?>
+--EXPECT--
23 tests/020-services-twitter-statuses-update.phpt
@@ -0,0 +1,23 @@
+--TEST--
+Services_Twitter_Statuses::update()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$update = '@joestump ' . $user . ' is playing with Services_Twitter.';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->update($update);
+
+var_dump(((string)$res->text == $update));
+
+$id = (string)$res->id;
+$fp = fopen('status.destroy', 'w');
+fwrite($fp, $id, strlen($id));
+fclose($fp);
+
+?>
+--EXPECT--
+bool(true)
28 tests/025-services-twitter-statuses-destroy.phpt
@@ -0,0 +1,28 @@
+--TEST--
+Services_Twitter_Statuses::destroy()
+--SKIPIF--
+<?php
+
+if (!file_exists('status.destroy')) {
+ echo "skip no status id found to destroy";
+}
+
+?>
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+
+$id = intval(file_get_contents('status.destroy'));
+$res = $twitter->statuses->destroy($id);
+
+var_dump((intval((string)$res->id) == $id));
+
+unlink('status.destroy');
+
+?>
+--EXPECT--
+bool(true)
39 tests/026-services-twitter-statuses-friends.phpt
@@ -0,0 +1,39 @@
+--TEST--
+Services_Twitter_Statuses::friends()
+--SKIPIF--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->friends();
+
+if (!count($res->user)) {
+ echo "skip $user has no friends";
+ exit;
+}
+
+?>
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->statuses->friends();
+
+if (!count($res->user)) {
+ echo "No users!\n";
+ exit;
+}
+
+foreach ($res->user as $user) {
+ if (!is_numeric((int)$user->id)) {
+ print_r($user);
+ }
+}
+
+?>
+--EXPECT--
30 tests/027-services-twitter-statuses-followers.phpt
@@ -0,0 +1,30 @@
+--TEST--
+Services_Twitter_Statuses::followers()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+try {
+ $twitter = new Services_Twitter($user, $pass);
+ $res = $twitter->statuses->followers();
+
+ if (!count($res->user)) {
+ echo "No users!\n";
+ print_r($res);
+ exit;
+ }
+
+ foreach ($res->user as $user) {
+ if (!is_numeric((int)$user->id)) {
+ print_r($user);
+ }
+ }
+
+} catch (Services_Twitter_Exception $e) {
+ echo $e . "\n";
+}
+
+?>
+--EXPECT--
30 tests/028-services-twitter-statuses-featured.phpt
@@ -0,0 +1,30 @@
+--TEST--
+Services_Twitter_Statuses::featured()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+try {
+ $twitter = new Services_Twitter($user, $pass);
+ $res = $twitter->statuses->featured();
+
+ if (!count($res->user)) {
+ echo "No featured users!\n";
+ print_r($res);
+ exit;
+ }
+
+ foreach ($res->user as $user) {
+ if (!is_numeric((int)$user->id)) {
+ print_r($user);
+ }
+ }
+
+} catch (Services_Twitter_Exception $e) {
+ echo $e . "\n";
+}
+
+?>
+--EXPECT--
19 tests/030-services-twitter-exception.phpt
@@ -0,0 +1,19 @@
+--TEST--
+Services_Twitter_Statuses::destroy()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+try {
+ $twitter = new Services_Twitter($user, $pass);
+ $res = $twitter->statuses->destroy(778371313);
+ var_dump($res);
+} catch (Services_Twitter_Exception $e) {
+ echo $e->getMessage();
+}
+
+?>
+--EXPECT--
+No status found with that ID.
23 tests/100-services-twitter-users-show.phpt
@@ -0,0 +1,23 @@
+--TEST--
+Services_Twitter_Users::show()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+try {
+ $twitter = new Services_Twitter($user, $pass);
+ $res = $twitter->users->show('kastner');
+ echo intval((string)$res->id) . "\n";
+
+ $res = $twitter->users->show('joe@joestump.net');
+ echo intval((string)$res->id) . "\n";
+} catch (Services_Twitter_Exception $e) {
+ echo $e->getMessage();
+}
+
+?>
+--EXPECT--
+627303
+4234581
39 tests/200-services-twitter-direct-messages.phpt
@@ -0,0 +1,39 @@
+--TEST--
+Services_Twitter::direct_messages()
+--SKIPIF--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->direct_messages();
+
+if (!count($res->direct_message)) {
+ echo "skip $user has no direct messages";
+ exit;
+}
+
+?>
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->direct_messages();
+
+if (!count($res->direct_message)) {
+ echo "No direct messages!\n";
+ exit;
+}
+
+foreach ($res->direct_message as $dm) {
+ if (!is_numeric((int)$dm->id)) {
+ print_r($dm);
+ }
+}
+
+?>
+--EXPECT--
24 tests/205-services-twitter-direct-messages-sent.phpt
@@ -0,0 +1,24 @@
+--TEST--
+Services_Twitter_DirectMessages::sent()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->direct_messages->sent();
+
+if (!count($res->direct_message)) {
+ echo "No direct messages!\n";
+ exit;
+}
+
+foreach ($res->direct_message as $dm) {
+ if (!is_numeric((int)$dm->id)) {
+ print_r($dm);
+ }
+}
+
+?>
+--EXPECT--
16 tests/210-services-twitter-direct-messages-new.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Services_Twitter_DirectMessages::new()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->direct_messages->new($aFriendOfMine, 'Hey! I have (' . $user . ') been playing with Services_Twitter');
+
+var_dump((intval((string)$res->id) > 0));
+
+?>
+--EXPECT--
+bool(true)
24 tests/215-services-twitter-direct-messages-destroy.phpt
@@ -0,0 +1,24 @@
+--TEST--
+Services_Twitter_DirectMessages::destroy()
+--SKIPIF--
+<?php
+
+require_once 'tests-config.php';
+if (!isset($directMessageToDelete) || !is_numeric($directMessageToDelete)) {
+ echo 'skip $directMessageToDelete is not set properly';
+}
+
+?>
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->direct_messages->destroy($directMessageToDelete);
+var_dump((intval((string)$res->id) == $directMessageToDelete));
+
+?>
+--EXPECT--
+bool(true)
16 tests/300-services-twitter-friendships-create.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Services_Twitter_Friendships::create()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->friendships->create('joestump');
+
+var_dump(((string)$res->screen_name == 'joestump'));
+
+?>
+--EXPECT--
+bool(true)
16 tests/305-services-twitter-friendships-destroy.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Services_Twitter_Friendships::destroy()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->friendships->destroy('joestump');
+
+var_dump(((string)$res->screen_name == 'joestump'));
+
+?>
+--EXPECT--
+bool(true)
15 tests/400-services-twitter-account-verify-credentials.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Services_Twitter_Account::verify_credentials()
+--FILE--
+<?php
+
+require_once 'tests-config.php';
+require_once 'Services/Twitter.php';
+
+$twitter = new Services_Twitter($user, $pass);
+$res = $twitter->account->verify_credentials();
+var_dump($res);
+
+?>
+--EXPECT--
+bool(true)
32 tests/tests-config.php.example
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Tests config file
+ *
+ * @author Joe Stump <joe@joestump.net>
+ * @package Services_Twitter
+ */
+
+// Uncomment the following code and add the correct information. All tests
+// will fail if you do not enter this information.
+// $user = 'XXXXXXX';
+// $pass = 'ZZZZZZZ';
+
+// Uncomment the following line of code to test direct messaging. You MUST
+// be friends with this username. Also, make sure to give them a heads up
+// before you start willy nilly sending them a bunch of test messages. Should
+// be a valid username or id.
+// $aFriendOfMine = 'XXXXXXX';
+
+// Uncomment the following line of code to test the destruction of direct
+// messages. You must put a direct message that was sent to $user above.
+// !!!WARNING!!! This direct message WILL be deleted permanently from Twitter!
+// $directMessageToDelete = '';
+
+set_include_path(realpath(dirname(__FILE__) . '/../') . ':' . get_include_path());
+
+if (!isset($user) || !strlen($user) || !isset($pass) || !strlen($pass)) {
+ die("Please set your username / password in the tests config file\n");
+}
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.