|
|
||
http://plagger.g.hatena.ne.jp/SweetPotato/20070919/tumblr
<?php /** * Tumblr Read APIをたたくらいぶらり * Zend_Service_Deliciousからぱくりとちゅう */ /** * @see Zend_Service_Abstract */ require_once 'Zend/Service/Abstract.php'; /** * @see Zend_Service_Exception */ require_once 'Zend/Service/Exception.php'; /** * @category Diggin * @package Diggin_Service_Tumblr * @subpackage Tumblr * @author */ class Diggin_Service_Tumblr_Read extends Zend_Service_Abstract { const API_LOCATION = '.tumblr.com/api/read'; /** * Zend_Http_Client Object * * @var Zend_Http_Client */ protected $_client; /** * target * * @var string */ protected $_target; /** * Microtime of last request * * @var float */ protected static $_lastRequestTime = 0; /** * Set target * * @param string $target subdomain,domain,URL * @return Zend_Service_Tumblr_Read */ public function setTarget($target) { $this->_target = (string) $target; return $this; } /** * Get target * * @return string $target */ public function getTarget() { return $this->_target; } public function __construct() { } public function getApiUrl() { $apiUrl = 'http://'.$this->getTarget().self::API_LOCATION; return $apiUrl; } public function getTotal() { $response = $this->makeRequest(); $rootNode = $response->documentElement; if ($rootNode->nodeName == 'tumblr') { $childNodes = $rootNode->childNodes; for ($i = 0; $i < $childNodes->length; $i++) { $currentNode = $childNodes->item($i); if ($currentNode->nodeName == 'posts') { (integer)$total = $currentNode->getAttribute('total'); } } } else { /** * @see Zend_Service_Delicious_Exception */ require_once 'Zend/Service/Delicious/Exception.php'; throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!'); } return $total; } //This is test method public function getAllPhotoUrl($type = null) { $num = 50; $parms['start'] = 0; $parms['num'] = 50; $parms['type'] = $type; (integer) $total = $this->getTotal(); (integer) $loop = $total / $num; $loop = floor($loop) + 1; $loop = 2;////←ここをコメントアウトすると多分しぬ $arrPhotoUrl = array(); for ($i = 0; $i < $loop; $i++) { if($i!=0){ $parms['start'] = $i * $num + 1; } $response = $this->makeRequest($parms); //DOM $xml = simplexml_import_dom($response); foreach($xml->posts->post as $post){ if($post['type'] == 'photo'){ foreach($post as $po){ if($po['max-width'] == '500'){ $arrPhotoUrl[] = $po; } } } } } return $arrPhotoUrl; } /** * Handles all GET requests to a web service * * @param string $path Path * @param array $parms Array of GET parameters * @return mixed decoded response from web service */ public function makeRequest(array $parms = array()) { // if previous request was made less then 1 sec ago // wait until we can make a new request $timeDiff = microtime(true) - self::$_lastRequestTime; if ($timeDiff < 1) { usleep((1 - $timeDiff) * 1000000); } $this->_client = self::getHttpClient(); $this->_client->setUri(self::getApiUrl()); if(isset($parms)){ $this->_client->setParameterGet($parms); } self::$_lastRequestTime = microtime(true); $response = $this->_client->request('GET'); if (!$response->isSuccessful()) { /** * @see Zend_Service_Delicious_Exception */ require_once 'Zend/Service/Exception.php'; throw new Zend_Service_Delicious_Exception("Http client reported an error: '{$response->getMessage()}'"); } $responseBody = $response->getBody(); $dom = new DOMDocument() ; if (!@$dom->loadXML($responseBody)) { /** * @see Zend_Service_Delicious_Exception */ require_once 'Zend/Service/Delicious/Exception.php'; throw new Zend_Service_Delicious_Exception('XML Error'); } return $dom; } } ///// $tumblr = new Diggin_Service_Tumblr_Read(); $tumblr->setTarget('fuba'); // $tumblr->setHttpClient($client); //print_r($tumblr->getAllPhotoUrl()); $photoUrls = $tumblr->getAllPhotoUrl(); foreach($photoUrls as $photoUrl){ echo $photoUrl."\n"; }
Sewana2011/08/19 12:53It's like you're on a moissin to save me time and money!
ggkohwiaz2011/08/19 21:28BZElw8 <a href="http://cxdmqtorldyf.com/">cxdmqtorldyf</a>
ijtdionftcz2011/08/21 00:28IbtcI9 , [url=http://qntarzelxeyn.com/]qntarzelxeyn[/url], [link=http://nvwpxrnlfbiz.com/]nvwpxrnlfbiz[/link], http://nphficzfhsxh.com/
uxhzztod2011/08/25 19:23hhSNha <a href="http://ihdzbljbifoz.com/">ihdzbljbifoz</a>
tmmfvtnupmi2011/08/31 02:356Drk8l , [url=http://egjenfsvvukj.com/]egjenfsvvukj[/url], [link=http://tooplyskzdgb.com/]tooplyskzdgb[/link], http://cubrbzqrdejp.com/
<?php /** * * @category Diggin * @package Diggin_Service * @subpackage Tumblr * @license MITあたり * @version $Id:$ */ /** * = how to use= * require_once 'Diggin/Service/Tumblr.php'; * $tumblr = new Diggin_Service_Tumblr(); * $tumblr->setAuth('sample@sample.com', 'password'); * $tumblr->setConfig(array('timeout'=> 500));//option * $tumblr->writeRegular('title', 'body messages'); */ /** * @see Zend_Http_Client */ require_once 'Zend/Http/Client.php'; /** * @see Zend_Http_Client_Exception */ require_once 'Zend/Http/Client/Exception.php'; /** * @category Diggin * @package Diggin_Service * @subpackage Tumblr * @author kazusuke <sasezaki@gmail.com> */ class Diggin_Service_Tumblr { const DIGGIN_SERVICE_TUMBLR = 'DIGGIN_SERVICE_TUMBLR'; /** * Zend_Http_Client Object * * @var Zend_Http_Client */ protected $_client; /** * Your Tumblr Email * * @var string */ protected $_tumblrEmail; /** * Your Tumblr Password * * @var string */ protected $_tumblrPassword; /** * Checking Type * * @var string */ protected $_checkType; /* * * */ protected $posts = array(); /** * Sets up character encoding, instantiates the HTTP client, and assigns the web service version * and testing parameters (if provided). * * @param string $tumblrEmail * @param string $$tumblrPassword * @param array (Zend_Http_Client)$config * @return void */ public function __construct($tumblrEmail = null, $tumblrPassword = null, $config = array()) { $this->_client = new Zend_Http_Client(); $this->_client->setConfig($config); $this->setAuth($tumblrEmail, $tumblrPassword); } /** * Set E-mail address & Password For Tumblr * * @param string $tumblrEmail * @param string $tumblrPassword * @return */ public function setAuth($tumblrEmail, $tumblrPassword) { $this->_tumblrEmail = (string) $tumblrEmail; $this->_tumblrPassword = (string) $tumblrPassword; return $this; } /** * config * * @param array (Zend_Http_Client)$config * @return */ public function setConfig($config = array()) { $this->_client->setConfig($config); return $this; } /** * Checking Option before Posting Tumblr * * @param $checkType (Full, STRICT, NONE) * @return Diggin_Service_Tumblr Provides a fluent interface */ public function setChecked($checkType = null) { $this->_checkType = $checkType; return $this; } /** * Private method that queries * * @param array $posts * @throws Zend_Http_Client_Exception * @throws Diggin_Service_Exception * @return Diggin_Service_Tumblr Provides a fluent interface */ private function write($posts, $generator = null) { $posts['email'] = $this->_tumblrEmail; $posts['password'] = $this->_tumblrPassword; foreach ($posts as $p => $var) { if(trim($var)) { $this->posts[strtolower($p)] = $var; } } if(!isset($generator)) { $generator = DIGGIN_SERVICE_TUMBLR; } elseif (isset($generator) && strlen($generator) <= 64) { $posts['generator'] = $generator; } else { /** * @see Diggin_Service_Exception */ require_once 'Diggin/Service/Exception.php'; $exception = new Diggin_Service_Exception('Generator Name is longed'); throw $exception; } $this->_client->setUri('http://www.tumblr.com/api/write'); $this->_client->setParameterPost($posts); $request = $this->_client->request('POST'); $response = $request->getStatus(); if ($response == 403) { throw new Zend_Http_Client_Exception('Your email address or password were incorrect'); } else if ($response == 400) { throw new Zend_Http_Client_Exception('There was at least one error while trying to save your post'); } else if ($request->isError()) { throw new Zend_Http_Client_Exception('The tumblr.com returned unknown status code'); } return $this; } /** * write by 'Regular' * * @param string $postTitle (optional) * @param string $postBody * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writeRegular($postTitle = null, $postBody = null) { $posts['type'] = 'regular'; if(trim($postTitle) == null && trim($postBody) == null) { /** * @see Diggin_Service_Exception */ require_once 'Diggin/Service/Exception.php'; $exception = new Diggin_Service_Exception('Requires at least one'); throw $exception; } $posts['title'] = $postTitle; $posts['body'] = $postBody; return $this->write($posts); } /** * write by 'Photo' * * @param string type ('source' or 'data') * @param string $sourceOrData * source - The URL of the photo to copy. * data - The binary data of the requested image. * @param string $caption (optional) * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writePhoto($type, $sourceOrData, $caption = null) { $posts['type'] = 'photo'; if (strtolower($type) == 'source'){ $posts['source'] = $sourceOrData; } elseif (strtolower($type) == 'data'){ $posts['data'] = $sourceOrData; } else { require_once 'Diggin/Service/Exception.php'; $exception = new Diggin_Service_Exception('Requires either source or data'); throw $exception; } $posts['caption'] = $caption; return $this->write($posts); } /** * write by 'quote' * * @param string $quote * @param string $source (optional) : <a href="source"></a> * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writeQuote($quote, $source = null) { $posts['type'] = 'quote'; $posts['quote'] = $quote; $posts['source'] = $source; return $this->write($posts); } /** * write by 'Link' * * @param string $name (optional) * @param string $url * @param string $descrption (optional) * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writeLink($name = null, $url, $descrption = null) { $posts['type'] = 'link'; $posts['name'] = $name; $posts['url'] = $url; $posts['descrption'] = $descrption; return $this->write($posts); } /** * write by 'conversation ' * * @param string $title (optional) * @param string $conversation * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writeConversation($title = null, $conversation) { $posts['type'] = 'conversation'; $posts['title'] = $title; $posts['conversation'] = $conversation; return $this->write($posts); } /** * write by 'Video' * * @param string $embed * @param string $caption (optional) * @return Diggin_Service_Tumblr Provides a fluent interface */ public function writeVideo($embed, $caption = null) { $posts['type'] = 'video'; $posts['embed'] = $embed; $posts['caption'] = $caption; return $this->write($posts); } }
<?php $file = "test2.jpg"; $email = 'test@test.org'; $password = 'jkondolove'; ///////////////////// require_once 'Zend/Http/Client.php'; require_once 'Pel/PelJpeg.php'; $jpeg = new PelJpeg($file); $posts['email'] = $email; $posts['password'] = $password; //$posts['type'] = 'regular'; //$posts['title'] = 'title'; //$posts['body'] = 'bodybody'; $posts['type'] = 'photo'; $posts['data'] = $jpeg->getBytes(); $client = new Zend_Http_Client(); $client->setUri('http://www.tumblr.com/api/write'); $client->setParameterPost($posts); $request = $client->request('POST'); $response = $request->getStatus(); if ($response == 201){echo "success!";};