Пример #1
0
 /** 
  * Makes a request to the Yelp API and returns the response
  * 
  * @param    $host    The domain host of the API 
  * @param    $path    The path of the APi after the domain
  * @return   The JSON response from the request      
  */
 function request($host, $path)
 {
     $unsigned_url = "http://" . $host . $path;
     // Token object built using the OAuth library
     $token = new OAuthToken($this->TOKEN, $this->TOKEN_SECRET);
     // Consumer object built using the OAuth library
     $consumer = new OAuthConsumer($this->CONSUMER_KEY, $this->CONSUMER_SECRET);
     // Yelp uses HMAC SHA1 encoding
     $signature_method = new OAuthSignatureMethod_HMAC_SHA1();
     $oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
     // Sign the request
     $oauthrequest->sign_request($signature_method, $consumer, $token);
     // Get the signed URL
     $signed_url = $oauthrequest->to_url();
     // Send Yelp API Call
     $ch = curl_init($signed_url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     $data = curl_exec($ch);
     curl_close($ch);
     return $data;
 }
Пример #2
0
 /**
  * pretty much a helper function to set up the request
  */
 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL)
 {
     @$parameters or $parameters = array();
     $defaults = array("oauth_version" => OAuthRequest::$version, "oauth_nonce" => OAuthRequest::generate_nonce(), "oauth_timestamp" => OAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key);
     if ($token) {
         $defaults['oauth_token'] = $token->key;
     }
     $parameters = array_merge($defaults, $parameters);
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
Пример #3
0
 /**
  * Format and sign an OAuth / API request
  */
 function oAuthRequest($url, $method, $parameters)
 {
     if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
         $url = "{$this->host}{$url}.{$this->format}";
     }
     $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
     $request->sign_request($this->sha1_method, $this->consumer, $this->token);
     switch ($method) {
         case 'GET':
             return $this->http($request->to_url(), 'GET');
         default:
             return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
     }
 }