Posted By

JustinCrossman on 06/16/08


Tagged

wrapper class php textmate api tumblr


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nb109


Tumblr API Wrapper


 / Published in: PHP
 

  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. curl_close($c);
  49. if($status == "201"){
  50. return true;
  51. }
  52. elseif($status == "403"){
  53. return false;
  54. }
  55. else{
  56. return "error: $return";
  57. }
  58. }
  59. else{
  60. return "error: cURL not installed";
  61. }
  62. }
  63.  
  64. function check($action){
  65. $accepted = array("authenticate","check-vimeo","check-audio");
  66. if(in_array($action,$accepted)){
  67. $data["email"] = $this->email;
  68. $data["password"] = $this->password;
  69. $data["generator"] = $this->generator;
  70. $data["action"] = $action;
  71. if(function_exists("curl_version")){
  72. $c = curl_init('http://www.tumblr.com/api/write');
  73. curl_setopt($c,CURLOPT_POST,true);
  74. curl_setopt($c,CURLOPT_POSTFIELDS,$data);
  75. curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
  76. $result = curl_exec($c);
  77. $status = curl_getinfo($c,CURLINFO_HTTP_CODE);
  78. curl_close($c);
  79. if($status == "200"){
  80. $status = true;
  81. }
  82. elseif($status == "403" || $status == "400"){
  83. $status = false;
  84. }
  85. return $status;
  86. }
  87. else{
  88. return "error: cURL not installed";
  89. }
  90. }
  91. }
  92.  
  93. }
  94.  
  95. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: evanwalsh on June 27, 2008

Hey, thanks for posting this! :]

You need to login to post a comment.