/www/include/lib_flickr_api_oauth.php

https://github.com/straup/parallel-flickr · PHP · 146 lines · 90 code · 45 blank · 11 comment · 7 complexity · 5c8bde763357c6d4685ab05cba170805 MD5 · raw file

  1. <?php
  2. # This uses lib_oauth for all the signing and building
  3. # URL crap but uses Flamework's lib_http for actually
  4. # talking to the network.
  5. loadlib("oauth");
  6. loadlib("http");
  7. #################################################################
  8. $GLOBALS['cfg']['flickr_api_endpoint'] = 'http://api.flickr.com/services/rest/';
  9. $GLOBALS['cfg']['flickr_oauth_endpoint'] = 'http://www.flickr.com/services/oauth/';
  10. #################################################################
  11. function flickr_api_oauth_get_request_token($args=array()){
  12. $keys = array(
  13. 'oauth_key' => $GLOBALS['cfg']['flickr_oauth_key'],
  14. 'oauth_secret' => $GLOBALS['cfg']['flickr_oauth_secret'],
  15. );
  16. $url = $GLOBALS['cfg']['flickr_oauth_endpoint'] . 'request_token/';
  17. $url = oauth_sign_get($keys, $url, $args, 'GET');
  18. $rsp = http_get($url);
  19. if (! $rsp['ok']){
  20. return $rsp;
  21. }
  22. $data = flickr_api_oauth_rsp_to_hash($rsp['body']);
  23. return array(
  24. 'ok' => 1,
  25. 'data' => $data,
  26. );
  27. }
  28. #################################################################
  29. function flickr_api_oauth_get_auth_url(&$args, &$user_keys){
  30. $keys = array(
  31. 'oauth_key' => $GLOBALS['cfg']['flickr_oauth_key'],
  32. 'oauth_secret' => $GLOBALS['cfg']['flickr_oauth_secret'],
  33. 'user_key' => $user_keys['oauth_token'],
  34. 'user_secret' => $user_keys['oauth_secret'],
  35. );
  36. $url = $GLOBALS['cfg']['flickr_oauth_endpoint'] . 'authorize/';
  37. $url = oauth_sign_get($keys, $url, $args, 'GET');
  38. return $url;
  39. }
  40. #################################################################
  41. function flickr_api_oauth_get_access_token(&$args, &$user_keys){
  42. $keys = array(
  43. 'oauth_key' => $GLOBALS['cfg']['flickr_oauth_key'],
  44. 'oauth_secret' => $GLOBALS['cfg']['flickr_oauth_secret'],
  45. 'user_key' => $user_keys['oauth_token'],
  46. 'user_secret' => $user_keys['oauth_secret'],
  47. );
  48. $url = $GLOBALS['cfg']['flickr_oauth_endpoint'] . 'access_token/';
  49. $url = oauth_sign_get($keys, $url, $args, 'GET');
  50. $rsp = http_get($url);
  51. if (! $rsp['ok']){
  52. return $rsp;
  53. }
  54. $data = flickr_api_oauth_rsp_to_hash($rsp['body']);
  55. return array(
  56. 'ok' => 1,
  57. 'data' => $data,
  58. );
  59. }
  60. #################################################################
  61. function flickr_api_oauth_call($method, $args, $more=array()){
  62. $keys = array(
  63. 'oauth_key' => $GLOBALS['cfg']['flickr_oauth_key'],
  64. 'oauth_secret' => $GLOBALS['cfg']['flickr_oauth_secret'],
  65. );
  66. if (isset($more['oauth_token'])){
  67. $keys['user_key'] = $more['oauth_token'];
  68. $keys['user_secret'] = $more['oauth_secret'];
  69. }
  70. $args['method'] = $method;
  71. $args['format'] = 'json';
  72. $args['nojsoncallback'] = 1;
  73. # Just keep things simple and assume we're always doing POSTs
  74. $url = oauth_sign_get($keys, $GLOBALS['cfg']['flickr_api_endpoint'], $args, 'POST');
  75. dumper($url);
  76. list($url, $postdata) = explode('?', $url, 2);
  77. $rsp = http_post($url, $postdata);
  78. if (! $rsp['ok']){
  79. return $rsp;
  80. }
  81. $json = json_decode($rsp['body'], 'as a hash');
  82. if (! $json){
  83. return array( 'ok' => 0, 'error' => 'failed to parse response' );
  84. }
  85. if ($json['stat'] != 'ok'){
  86. return array( 'ok' => 0, 'error' => $json['message']);
  87. }
  88. unset($json['stat']);
  89. return array( 'ok' => 1, 'data' => $json );
  90. }
  91. #################################################################
  92. function flickr_api_oauth_rsp_to_hash($rsp){
  93. $data = array();
  94. foreach (explode("&", $rsp) as $bit){
  95. list($k, $v) = explode('=', $bit, 2);
  96. $data[urldecode($k)] = urldecode($v);
  97. }
  98. return $data;
  99. }
  100. #################################################################
  101. ?>