Tumblr API包装器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tumblr API包装器相关的知识,希望对你有一定的参考价值。

  1. <?php
  2.  
  3. class Tumblr{
  4.  
  5. /*
  6. Tumblr API PHP class by Evan Walsh
  7. http://code.evanwalsh.net/Projects/Tumblr
  8. */
  9.  
  10. function read($url,$json = false){
  11. $url = "$url/api/read";
  12. if($json){
  13. $url .= "/json";
  14. }
  15. if(ini_get("allow_url_fopen")){
  16. $output = file_get_contents($url);
  17. }
  18. elseif(function_exists("curl_version")){
  19. $c = curl_init($url);
  20. curl_setopt($c,CURLOPT_HEADER,1);
  21. curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
  22. $output = curl_exec($c);
  23. }
  24. else{
  25. $output = "error: cannot fetch file";
  26. }
  27. return $output;
  28. }
  29.  
  30. function init($email,$password,$generator = "Tumblr PHP class"){
  31. $this->email = $email;
  32. $this->password = $password;
  33. $this->generator = $generator;
  34. }
  35.  
  36. function post($data){
  37. if(function_exists("curl_version")){
  38. $data["email"] = $this->email;
  39. $data["password"] = $this->password;
  40. $data["generator"] = $this->generator;
  41. $request = http_build_query($data);
  42. $c = curl_init('http://www.tumblr.com/api/write');
  43. curl_setopt($c,CURLOPT_POST,true);
  44. curl_setopt($c,CURLOPT_POSTFIELDS,$request);
  45. curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
  46. $return = curl_exec($c);
  47. $status = curl_getinfo($c,CURLINFO_HTTP_CODE);
  48. if($status == "201"){
  49. return true;
  50. }
  51. elseif($status == "403"){
  52. return false;
  53. }
  54. else{
  55. return "error: $return";
  56. }
  57. }
  58. else{
  59. return "error: cURL not installed";
  60. }
  61. }
  62.  
  63. function check($action){
  64. $accepted = array("authenticate","check-vimeo","check-audio");
  65. if(in_array($action,$accepted)){
  66. $data["email"] = $this->email;
  67. $data["password"] = $this->password;
  68. $data["generator"] = $this->generator;
  69. $data["action"] = $action;
  70. if(function_exists("curl_version")){
  71. $c = curl_init('http://www.tumblr.com/api/write');
  72. curl_setopt($c,CURLOPT_POST,true);
  73. curl_setopt($c,CURLOPT_POSTFIELDS,$data);
  74. curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
  75. $result = curl_exec($c);
  76. $status = curl_getinfo($c,CURLINFO_HTTP_CODE);
  77. if($status == "200"){
  78. $status = true;
  79. }
  80. elseif($status == "403" || $status == "400"){
  81. $status = false;
  82. }
  83. return $status;
  84. }
  85. else{
  86. return "error: cURL not installed";
  87. }
  88. }
  89. }
  90.  
  91. }
  92.  
  93. ?>

以上是关于Tumblr API包装器的主要内容,如果未能解决你的问题,请参考以下文章

为 Javascript API 创建飞镖包装器的好方法是啥?

如何使用 SWIG 在 C++ API 上生成 C 包装器? [复制]

用于 kernel32.dll API 的包装器 C#

亚马逊产品 api 的 Python 包装器 [重复]

(js) 使用 api 包装器编写异步函数

ruby Propublica的Congress API的API包装器