Skip to content

Loading…

Inclusion of microblogging services Status.net #8

Open
wants to merge 5 commits into from
This page is out of date. Refresh to see the latest.
Showing with 103 additions and 54 deletions.
  1. +1 −1 readme.txt
  2. +102 −53 twitter.class.php
View
2 readme.txt
@@ -16,7 +16,7 @@ My PHP blog: http://phpfashion.com
Requirements
------------
- PHP (version 5 or better)
-- cURL extension
+- enabled allow_url_fopen
Usage
View
155 twitter.class.php
@@ -11,7 +11,7 @@
* @license New BSD License
* @link http://phpfashion.com/
* @see http://dev.twitter.com/doc
- * @version 2.0
+ * @version 2.2
*/
class Twitter
{
@@ -45,6 +45,27 @@ class Twitter
/** @var OAuthConsumer */
private $token;
+ /**
+ * @var string
+ * @uses microblogging service used in class.
+ * @example twitter, twitter.com or null to use Twitter service
+ * @example others values will be treated as StatusNet services (including sites like identi.ca that run on it).
+ * @usedby getSearchUrl, clickable
+ * @see http://status.net/wiki/TwitterCompatibleAPI
+ */
+ private $service;
+
+ /**
+ * Set service to use
+ * @param string $service
+ */
+ public function setService($service) {
+ if (!$service || in_array(strtolower($service), array('twitter', 'twitter.com')))
+ $this->service = 'twitter';
+ else
+ $this->service = $service;
+ }
+
/**
@@ -53,17 +74,18 @@ class Twitter
* @param string app secret
* @param string optional access token
* @param string optinal access token secret
- * @throws TwitterException when CURL extension is not loaded
+ * @param string microblogging service
+ * @throws TwitterException when allow_url_fopen is not enabled
*/
- public function __construct($consumerKey = NULL, $consumerSecret = NULL, $accessToken = NULL, $accessTokenSecret = NULL)
+ public function __construct($consumerKey = NULL, $consumerSecret = NULL, $accessToken = NULL, $accessTokenSecret = NULL, $service = NULL)
{
- if (!extension_loaded('curl')) {
- throw new TwitterException('PHP extension CURL is not loaded.');
+ if (!ini_get('allow_url_fopen')) {
+ throw new TwitterException('PHP directive allow_url_fopen is not enabled.');
}
-
$this->signatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer($consumerKey, $consumerSecret);
$this->token = new OAuthConsumer($accessToken, $accessTokenSecret);
+ $this->setService($service);
}
@@ -172,8 +194,9 @@ public function destroy($id)
*/
public function search($query, $flags = self::JSON)
{
+
return $this->request(
- 'http://search.twitter.com/search.' . self::getFormat($flags),
+ self::getSearchUrl() . '/search.' . self::getFormat($flags),
is_array($query) ? $query : array('q' => $query),
'GET'
)->results;
@@ -201,43 +224,30 @@ public function request($request, $data = NULL, $method = 'POST')
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $request, $data);
$request->sign_request($this->signatureMethod, $this->consumer, $this->token);
- $curl = curl_init();
- 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:'));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
- curl_setopt($curl, CURLOPT_USERAGENT, 'Twitter for PHP');
- if ($method === 'POST') {
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $request->to_postdata());
- curl_setopt($curl, CURLOPT_URL, $request->get_normalized_http_url());
- } else {
- curl_setopt($curl, CURLOPT_URL, $request->to_url());
- }
-
- $result = curl_exec($curl);
- if (curl_errno($curl)) {
- throw new TwitterException('Server error: ' . curl_error($curl));
- }
-
- $type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
- if (strpos($type, 'xml')) {
- $payload = @simplexml_load_string($result); // intentionally @
+ $options = array(
+ 'method' => $method,
+ 'timeout' => 20,
+ 'content' => $method === 'POST' ? $request->to_postdata() : NULL,
+ 'user_agent' => 'Twitter for PHP',
+ );
- } elseif (strpos($type, 'json')) {
- $payload = @json_decode($result); // intentionally @
+ $f = @fopen($method === 'POST' ? $request->get_normalized_http_url() : $request->to_url(),
+ 'r', FALSE, stream_context_create(array('http' => $options)));
+ if (!$f) {
+ throw new TwitterException('Server error');
}
+ $result = stream_get_contents($f);
+ $payload = @simplexml_load_string($result); // intentionally @
if (empty($payload)) {
- throw new TwitterException('Invalid server response');
- }
-
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($code >= 400) {
- throw new TwitterException(isset($payload->error) ? $payload->error : "Server error #$code", $code);
+ $payload = @json_decode($result); // intentionally @
+ if (empty($payload)) {
+ throw new TwitterException('Invalid server response');
+ }
}
-
+ if ($this->service)
+ foreach ($payload->results as $key => $value)
+ $payload->results[$key]->service = $this->service;
return $payload;
}
@@ -281,8 +291,9 @@ public function cachedRequest($request, $data)
* @param string
* @return string
*/
- public static function clickable($s)
+ public function clickable($s, $service = NULL)
{
+ if ($service) $this->service = $service;
return preg_replace_callback(
'~(?<!\w)(https?://\S+\w|www\.\S+\w|@\w+|#\w+|<>&)~u',
array(__CLASS__, 'clickableCallback'),
@@ -292,15 +303,15 @@ public static function clickable($s)
- private static function clickableCallback($m)
+ private function clickableCallback($m)
{
$m = htmlspecialchars($m[1]);
if ($m[0] === '#') {
- $m = substr($m, 1);
- return "<a href='http://twitter.com/search?q=%23$m'>#$m</a>";
+ $m = substr($m, 1);
+ return "<a href='" . $this->getClickableTagUrl($m) . "'>#$m</a>";
} elseif ($m[0] === '@') {
$m = substr($m, 1);
- return "@<a href='http://www.twitter.com/$m'>$m</a>";
+ return "@<a href='" . $this->getClickableUserUrl($m) . "'>$m</a>";
} elseif ($m[0] === 'w') {
return "<a href='http://$m'>$m</a>";
} elseif ($m[0] === 'h') {
@@ -319,14 +330,8 @@ private static function clickableCallback($m)
*/
private function shortenUrl($m)
{
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, 'http://is.gd/api.php?longurl=' . urlencode($m[0]));
- curl_setopt($curl, CURLOPT_HEADER, FALSE);
- curl_setopt($curl, CURLOPT_TIMEOUT, 20);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- $result = curl_exec($curl);
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- return curl_errno($curl) || $code >= 400 ? $m[0] : $result;
+ $f = @fopen('http://is.gd/api.php?longurl=' . urlencode($m[0]), 'r');
+ return $f ? stream_get_contents($f) : $m[0];
}
@@ -342,6 +347,50 @@ private static function getFormat($flag)
}
}
+
+
+ private function getRquestUrl($service = NULL)
+ {
+ if (!$service) $service = $this->service;
+ if ($service == 'twitter')
+ return 'http://api.twitter.com/1';
+ else
+ return "http://$service/api";
+ }
+
+
+
+ private function getSearchUrl($service = NULL)
+ {
+ if (!$service) $service = $this->service;
+ if ($service == 'twitter')
+ return 'http://search.twitter.com';
+ else
+ return "http://$service/api";
+ }
+
+
+
+ private function getClickableTagUrl($tag, $service = NULL)
+ {
+ if (!$service) $service = $this->service;
+ if ($service == 'twitter')
+ return "http://twitter.com/#!/search/%23$tag";
+ else
+ return "http://$service/search/notice?q=%23$tag";
+ }
+
+
+
+ public function getClickableUserUrl($user, $service = NULL)
+ {
+ if (!$service) $service = $this->service;
+ if ($service == 'twitter')
+ return "http://twitter.com/#!/$user";
+ else
+ return "http://$service/$user";
+ }
+
}
@@ -351,4 +400,4 @@ private static function getFormat($flag)
*/
class TwitterException extends Exception
{
-}
+}
Something went wrong with that request. Please try again.