PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/TumblrBlog.php

https://github.com/bdalziel/tumblwrapper
PHP | 150 lines | 124 code | 25 blank | 1 comment | 11 complexity | bfc286296959d38ccefdc8aaa7eb701e MD5 | raw file
  1. <?php
  2. require('TumblrPost.php');
  3. class TumblrBlog {
  4. const TUMBLR_API_HOST = "http://api.tumblr.com";
  5. const TUMBLR_API_KEY = "EonZSbH566lKrQIcXzx8W1Tt3DcQ59tJhcmRTdXsEnjhodX9k2";
  6. const TUMBLR_PAGE_SIZE = 20;
  7. const TUMBLR_API_TIMEOUT = 5;
  8. protected $blogShortName;
  9. public function __construct ($blogShortName) {
  10. $this->blogShortName = $blogShortName;
  11. }
  12. public function getPost ($id) {
  13. $url = $this->getPostsUrl(null, $id);
  14. $response = $this->executeQuery($url);
  15. return $this->parsePosts($response);
  16. }
  17. public function getPosts ($page = 1) {
  18. $url = $this->getPostsUrl($page);
  19. $response = $this->executeQuery($url);
  20. return $this->parsePosts($response);
  21. }
  22. public function getBlogName () {
  23. $url = $this->getBlogInfoUrl();
  24. $response = $this->executeQuery($url);
  25. if (array_key_exists('blog', $response)) {
  26. $data = $response['blog'];
  27. return strval($data['title']);
  28. }
  29. }
  30. public function getPostCount () {
  31. $url = $this->getBlogInfoUrl();
  32. $response = $this->executeQuery($url);
  33. if (array_key_exists('blog', $response)) {
  34. $data = $response['blog'];
  35. return (int) strval($data['posts']);
  36. }
  37. }
  38. public function getUrl () {
  39. return "blog.php";
  40. }
  41. public function getPageCount () {
  42. return (int) ceil($this->getPostCount() / self::TUMBLR_PAGE_SIZE);
  43. }
  44. protected function parsePost ($data) {
  45. return new TumblrPost($data);
  46. }
  47. protected function parsePosts ($data) {
  48. $posts = array();
  49. if (is_array($data) && array_key_exists('posts', $data)) {
  50. $data = $data['posts'];
  51. foreach ($data as $post) {
  52. $post = $this->parsePost($post);
  53. if ($post) {
  54. $posts[] = $post;
  55. }
  56. }
  57. }
  58. return $posts;
  59. }
  60. protected function getBlogInfoUrl () {
  61. $apiUrl = implode('/', array($this->getBaseBlogApiUrl(),
  62. 'info'));
  63. $urlParams = $this->paramsToString(array('api_key' => self::TUMBLR_API_KEY));
  64. return implode('?', array($apiUrl, $urlParams));
  65. }
  66. protected function getPostsUrl ($page = null, $id = null) {
  67. $apiUrl = implode('/', array($this->getBaseBlogApiUrl(),
  68. 'posts'));
  69. $urlParams = array_merge(array('api_key' => self::TUMBLR_API_KEY),
  70. (array) $this->getApiPageParams($page),
  71. (array) $this->getApiPostIdParam($id));
  72. $urlParams = $this->paramsToString($urlParams);
  73. return implode('?', array($apiUrl, $urlParams));
  74. }
  75. protected function getApiPageParams ($page) {
  76. $params = array();
  77. if ($page > 1) {
  78. $params['offset'] = (int) ($page - 1)*self::TUMBLR_PAGE_SIZE;
  79. }
  80. return $params;
  81. }
  82. protected function getApiPostIdParam ($id) {
  83. $param = array();
  84. if ($id && is_numeric($id)) {
  85. $param['id'] = $id;
  86. }
  87. return $param;
  88. }
  89. protected function paramsToString ($params) {
  90. $urlParams = array();
  91. foreach ($params as $paramKey => $paramValue) {
  92. $urlParams[] = $paramKey . '=' . $paramValue;
  93. }
  94. return implode('&', $urlParams);
  95. }
  96. protected function getBaseBlogApiUrl () {
  97. return implode('/',
  98. array(self::TUMBLR_API_HOST,
  99. 'v2',
  100. 'blog',
  101. $this->getBaseHostName()));
  102. }
  103. protected function getBaseHostName () {
  104. return $this->blogShortName . '.tumblr.com';
  105. }
  106. protected function executeQuery ($query, $timeout = 10) {
  107. $ch = curl_init();
  108. curl_setopt($ch, CURLOPT_URL, $query);
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  110. curl_setopt($ch, CURLOPT_TIMEOUT, self::TUMBLR_API_TIMEOUT);
  111. $data = curl_exec($ch);
  112. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  113. curl_close($ch);
  114. // Todo: Throw exceptions
  115. if ($httpcode>=200 && $httpcode<300) {
  116. $data = json_decode($data, true);
  117. if (array_key_exists('response', $data)) {
  118. $data = $data['response'];
  119. }
  120. return $data;
  121. }
  122. return null;
  123. }
  124. }
  125. ?>