Mercurial > hg > ywww
comparison goodreads/GoodreadsAPI.php @ 6:077b0a0a3e6d
remaining originals according to dependency walk
| author | Robert Boland <robert@markup.co.uk> |
|---|---|
| date | Thu, 16 Feb 2017 22:29:02 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 5:55445b456ad0 | 6:077b0a0a3e6d |
|---|---|
| 1 <?php | |
| 2 | |
| 3 /** | |
| 4 * @author Sachin Khosla - @realin | |
| 5 * @desc This is the wrapper class, contains all the high level functions | |
| 6 * responsible of making all the calls to & fro to twitter | |
| 7 * | |
| 8 * modified by @YourNextRead to integrate with goodreads | |
| 9 */ | |
| 10 | |
| 11 | |
| 12 require_once 'OAuth.php'; | |
| 13 require_once 'config.php'; | |
| 14 | |
| 15 class GoodreadsAPI | |
| 16 { | |
| 17 | |
| 18 private $requestTokenURL = 'http://www.goodreads.com/oauth/request_token'; | |
| 19 private $accessTokenURL = 'http://www.goodreads.com/oauth/access_token'; | |
| 20 private $loginURL = 'http://www.goodreads.com/oauth/authorize'; | |
| 21 private $consumer,$token,$result; | |
| 22 public $return_code; | |
| 23 | |
| 24 | |
| 25 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) | |
| 26 { | |
| 27 // define the supported SHA1 method | |
| 28 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1(); | |
| 29 | |
| 30 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret); | |
| 31 | |
| 32 if (!empty($oauth_token) && !empty($oauth_token_secret)) | |
| 33 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret); | |
| 34 else | |
| 35 $this->token = NULL; | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * @param <string> $verify is the OAUTH_VERIFIER passed in the callback url | |
| 40 * @return access token | |
| 41 */ | |
| 42 | |
| 43 function getAccessToken($verify = '' ) | |
| 44 { | |
| 45 $parameters = array(); | |
| 46 if ($verify != '') | |
| 47 { | |
| 48 $data['oauth_verifier'] = $verify; | |
| 49 } | |
| 50 $request = $this->makeRequest($this->accessTokenURL,false, $data); | |
| 51 $token = OAuthUtil::parse_parameters($request); | |
| 52 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); | |
| 53 return $token; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * | |
| 58 * @param <string> $url URL to send the get request | |
| 59 * @param <array> $data anydata to be sent | |
| 60 */ | |
| 61 | |
| 62 function doGet($url, $data = array()) | |
| 63 { | |
| 64 | |
| 65 $response = $this->makeRequest($url, false, $data); | |
| 66 return $response; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * | |
| 71 * @param <string> $url URL to send the POST request | |
| 72 * @param <array> $data anydata to be sent | |
| 73 */ | |
| 74 | |
| 75 function doPost($url, $data = array()) | |
| 76 { | |
| 77 $response = $this->makeRequest($url, true, $data); | |
| 78 return $response; | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * gets the request token for the first time | |
| 83 */ | |
| 84 function getRequestToken($oauth_callback = NULL) | |
| 85 { | |
| 86 $params = array(); | |
| 87 if (!empty($oauth_callback)) | |
| 88 { | |
| 89 $params['oauth_callback'] = $oauth_callback; | |
| 90 } | |
| 91 $request = $this->makeRequest($this->requestTokenURL,false, $params); | |
| 92 $token = OAuthUtil::parse_parameters($request); | |
| 93 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); | |
| 94 return $token; | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Creates the Authorization/login URL | |
| 99 */ | |
| 100 function getLoginURL($token) | |
| 101 { | |
| 102 $token = $token['oauth_token']; | |
| 103 return $this->loginURL . "?oauth_token={$token}"; | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * Prepares the request for the CURL | |
| 108 */ | |
| 109 function makeRequest($url, $is_post = false, $data = array()) | |
| 110 { | |
| 111 | |
| 112 $method = ($is_post == true)?'POST':'GET'; | |
| 113 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $data); | |
| 114 $request->sign_request($this->sha1_method, $this->consumer, $this->token); | |
| 115 | |
| 116 if($is_post === true) | |
| 117 { | |
| 118 return $this->makeCurl($request->get_normalized_http_url(), $method, $request->to_postdata()); | |
| 119 } | |
| 120 else | |
| 121 return $this->makeCurl($request->to_url()); | |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Does all the CURL request, capable of sending both GET & POST requests | |
| 126 */ | |
| 127 private function makeCurl($url,$is_post = false,$data=null) | |
| 128 { | |
| 129 $curl = curl_init(); | |
| 130 curl_setopt($curl, CURLOPT_USERAGENT, 'GoodreadsOAuth v0.2.0-beta2'); | |
| 131 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
| 132 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); | |
| 133 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
| 134 | |
| 135 if (!empty($data) && $is_post == true) | |
| 136 { | |
| 137 curl_setopt($curl, CURLOPT_POST, TRUE); | |
| 138 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
| 139 } | |
| 140 | |
| 141 curl_setopt($curl, CURLOPT_URL, $url); | |
| 142 $this->result = curl_exec($curl); | |
| 143 $this->return_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| 144 curl_close($curl); | |
| 145 | |
| 146 return $this->result; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 | |
| 151 ?> |
