Tumblr API Wrapper


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.