Diffs
Services_Bitly/trunk/example.php
@@ -1,45 +0,0 @@
-<?php
-/**
- * example.php
- *
- */
-
-require_once 'Services/Bitly.php';
-
-$login = 'Bitlyのアカウント';
-$apikey = 'BitlyのAPI Key';
-
-try {
- $bitly = new Services_Bitly($login,$apikey);
- $shorten = $bitly->shorten("http://openpear.org/package/Services_Bitly");
-} catch (Services_Bitly_Exception $e) {
- echo $e->getMessage();
-}
-
-try {
- $bitly = new Services_Bitly($login,$apikey);
- $expand = $bitly->expand($shorten);
-} catch (Services_Bitly_Exception $e) {
- echo $e->getMessage();
-}
-
-
-
-// j.mp対応
-
-try {
- $bitly = new Services_Bitly($login,$apikey);
- $bitly->setBaseDomain('j.mp');
- $shorten = $bitly->shorten("http://openpear.org");
-} catch (Services_Bitly_Exception $e) {
- echo $e->getMessage();
-}
-
-try {
- $bitly = new Services_Bitly($login,$apikey);
- $bitly->setBaseDomain('j.mp');
- $expand = $bitly->expand($shorten);
-} catch (Services_Bitly_Exception $e) {
- echo $e->getMessage();
-}
-
Services_Bitly/trunk/Services/Bitly/example.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * example.php
+ *
+ */
+
+require_once 'Services/Bitly.php';
+
+$login = 'Bitlyのアカウント';
+$apikey = 'BitlyのAPI Key';
+
+try {
+ $bitly = new Services_Bitly($login,$apikey);
+ $shorten = $bitly->shorten("http://openpear.org/package/Services_Bitly");
+} catch (Services_Bitly_Exception $e) {
+ echo $e->getMessage();
+}
+
+try {
+ $bitly = new Services_Bitly($login,$apikey);
+ $expand = $bitly->expand($shorten);
+} catch (Services_Bitly_Exception $e) {
+ echo $e->getMessage();
+}
+
+
+
+// j.mp対応
+
+try {
+ $bitly = new Services_Bitly($login,$apikey);
+ $bitly->setBaseDomain('j.mp');
+ $shorten = $bitly->shorten("http://openpear.org");
+} catch (Services_Bitly_Exception $e) {
+ echo $e->getMessage();
+}
+
+try {
+ $bitly = new Services_Bitly($login,$apikey);
+ $bitly->setBaseDomain('j.mp');
+ $expand = $bitly->expand($shorten);
+} catch (Services_Bitly_Exception $e) {
+ echo $e->getMessage();
+}
+
属性に変更があったパス: Services_Bitly/trunk/Services/Bitly/example.php
___________________________________________________________________
追加: svn:eol-style
+ native
Services_Bitly/trunk/Services/Bitly.php
@@ -25,10 +25,11 @@
const FORMAT_JSON = 'json';
const FORMAT_XML = 'xml';
+ const FORMAT_TXT = 'txt';
- const API_VERSION = '2.0.1';
+ const API_VERSION = 'v3';
- const VERSION = '0.2.0';
+ const VERSION = '0.3.0';
/**
@@ -67,13 +68,6 @@
private $baseUrl;
/**
- * regex string
- *
- * @var string
- */
- private $regexString;
-
- /**
* Default constructor
*
* @return void
@@ -87,8 +81,8 @@
$this->setLogin($login);
$this->setApikey($apikey);
$this->setFormat($format);
- $this->setBaseDomain($domain);
$this->setApiVersion(self::API_VERSION);
+ $this->setBaseDomain($domain);
}
/**
@@ -101,14 +95,14 @@
*/
public function shorten($longurl)
{
+ $queryParameters = array(
+ 'login' => $this->login,
+ 'apiKey' => $this->apiKey,
+ 'uri' => $longurl,
+ 'format' => $this->format,
+ );
- $apiurl = $this->baseUrl . '/shorten?'
- . 'version=' . $this->apiVersion
- . '&longUrl=' . urlencode($longurl)
- . '&login=' . $this->login
- . '&apiKey=' . $this->apiKey
- . '&format=' . $this->format
- . '';
+ $apiurl = $this->baseUrl . '/shorten?' . http_build_query($queryParameters);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiurl);
@@ -125,52 +119,31 @@
if($this->format === self::FORMAT_JSON) {
- $json = json_decode($response,true);
+ $json = json_decode($response, true);
- if($json['errorCode'] === 0 && $json['statusCode'] === 'OK') {
-
- if($json['results'][$longurl]['errorCode'] !== null) {
-
- throw new Services_Bitly_Exception($json['results'][$longurl]['errorMessage'], $json['results'][$longurl]['errorCode']);
-
- }else{
-
- return $json['results'][$longurl]['shortUrl'];
-
- }
-
- }else{
-
- throw new Services_Bitly_Exception($json['errorMessage'], $json['errorCode']);
-
+ if ($json['status_code'] == "200" && $json['status_txt'] == "OK") {
+ return $json['data']['url'];
+ } else {
+ throw new Services_Bitly_Exception($json['status_txt'], $json['status_code']);
}
}
if($this->format === self::FORMAT_XML) {
- require_once 'XML/Unserializer.php';
+ $xml = simplexml_load_string($response);
- $unserializer = new XML_Unserializer(array(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => 'parseAttributes'));
- $unserializer->unserialize($response);
- $xml = $unserializer->getUnserializedData();
+ if ($xml->status_code == "200" && $xml->status_txt == 'OK') {
+ return $xml->data->url;
+ } else {
+ throw new Services_Bitly_Exception($xml->status_txt, $xml->status_code);
+ }
+ }
-
- if($xml['errorCode'] == 0 && $xml['statusCode'] == 'OK') {
-
- if(is_array($xml['results'][$longurl])) {
-
- throw new Services_Bitly_Exception($xml['results'][$longurl]['errorMessage'], (int)($xml['results'][$longurl]['errorCode']));
-
- }else{
-
- return $xml['results']['nodeKeyVal']['shortUrl'];
-
- }
-
- }else{
-
- throw new Services_Bitly_Exception($xml['errorMessage'], (int)($xml['errorCode']));
-
+ if ($this->format === self::FORMAT_TXT) {
+ if ($response) {
+ return $response;
+ } else {
+ throw new Services_Bitly_Exception('UNKNOWN ERROR. if more information: set fomat json or xml.');
}
}
}
@@ -185,19 +158,15 @@
*/
public function expand($shorturl)
{
+ $queryParameters = array(
+ 'login' => $this->login,
+ 'apiKey' => $this->apiKey,
+ 'shortUrl' => trim($shorturl),
+ 'format' => $this->format,
+ );
- if (!preg_match("/^$this->regexString/", $shorturl)) {
- throw new Services_Bitly_Exception("URL domain you tried to expand was invalid.");
- }
+ $apiurl = $this->baseUrl . '/expand?' . http_build_query($queryParameters);
- $apiurl = $this->baseUrl . '/expand?'
- . 'version=' . $this->apiVersion
- . '&shortUrl=' . urlencode($shorturl)
- . '&login=' . $this->login
- . '&apiKey=' . $this->apiKey
- . '&format=' . $this->format
- . '';
-
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiurl);
curl_setopt($curl, CURLOPT_HEADER, false);
@@ -213,55 +182,31 @@
if($this->format === self::FORMAT_JSON) {
- $json = json_decode($response,true);
+ $json = json_decode($response, true);
- $userhash = preg_replace("/^$this->regexString/", "", $shorturl);
+ if ($json['status_code'] == "200" && $json['status_txt'] === 'OK') {
+ return $json['data']['expand'][0]['long_url'];
+ } else {
+ throw new Services_Bitly_Exception($json['status_txt'], $json['status_code']);
+ }
+ }
- if($json['errorCode'] === 0 && $json['statusCode'] === 'OK') {
+ if ($this->format === self::FORMAT_XML) {
- if(is_array($json['results'][$userhash]['longUrl'])) {
+ $xml = simplexml_load_string($response);
- throw new Services_Bitly_Exception($json['results'][$userhash]['longUrl']['errorMessage'], $json['results'][$userhash]['longUrl']['errorCode']);
-
- }else{
-
- return $json['results'][$userhash]['longUrl'];
-
- }
-
- }else{
-
- throw new Services_Bitly_Exception($json['errorMessage'], $json['errorCode']);
-
+ if ($xml->status_code == "200" && $xml->status_txt == 'OK') {
+ return $xml->data->entry->long_url;
+ } else {
+ throw new Services_Bitly_Exception($xml->statas_txt, $xml->status_code);
}
}
- if($this->format === self::FORMAT_XML) {
-
- require_once 'XML/Unserializer.php';
-
- $unserializer = new XML_Unserializer(array(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => 'parseAttributes'));
- $unserializer->unserialize($response);
- $xml = $unserializer->getUnserializedData();
-
- $userhash = preg_replace("/^$this->regexString/", "", $shorturl);
-
- if($xml['errorCode'] == 0 && $xml['statusCode'] == 'OK') {
-
- if(is_array($xml['results'][$userhash]['longUrl'])) {
-
- throw new Services_Bitly_Exception($xml['results'][$userhash]['longUrl']['errorMessage'], (int)($xml['results'][$userhash]['longUrl']['errorCode']));
-
- }else{
-
- return $xml['results'][$userhash]['longUrl'];
-
- }
-
- }else{
-
- throw new Services_Bitly_Exception($xml['errorMessage'], (int)($xml['errorCode']));
-
+ if ($this->format === self::FORMAT_TXT) {
+ if ($response) {
+ return $response;
+ } else {
+ throw new Services_Bitly_Exception('UNKNOWN ERROR. if more information: set fomat json or xml.');
}
}
}
@@ -321,15 +266,14 @@
$this->baseDomain = (string) $domain;
switch($domain) {
case self::DOMAIN_BITLY:
- $this->baseUrl = self::API_URL_BITLY;
+ $this->baseUrl = self::API_URL_BITLY . "/" . $this->apiVersion;
break;
case self::DOMAIN_JMP:
- $this->baseUrl = self::API_URL_JMP;
+ $this->baseUrl = self::API_URL_JMP . "/" . $this->apiVersion;;
break;
default:
- $this->baseUrl = self::API_URL_BITLY;
+ $this->baseUrl = self::API_URL_BITLY . "/" . $this->apiVersion;;
}
- $this->regexString = (string) 'http:\/\/' . $domain . '\/';
}
}