Пример #1
0
 /**
  * @param $selected_call
  * @param $method_type
  * @param $params
  * @return string
  * @throws W3tcWpHttpException
  */
 private function execute($selected_call, $method_type, $params)
 {
     //increase the http request timeout
     add_filter('http_request_timeout', array($this, 'filter_timeout_time'));
     add_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
     $consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
     // the endpoint for your request
     $endpoint = "{$this->netdnarws_url}/{$this->alias}{$selected_call}";
     //parse endpoint before creating OAuth request
     $parsed = parse_url($endpoint);
     if (array_key_exists("parsed", $parsed)) {
         parse_str($parsed['query'], $params);
     }
     //generate a request from your consumer
     $req_req = W3tcOAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
     //sign your OAuth request using hmac_sha1
     $sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
     $req_req->sign_request($sig_method, $consumer, NULL);
     $request = array();
     $request['sslverify'] = false;
     $request['method'] = $method_type;
     if ($method_type == "POST") {
         $request['body'] = $req_req->to_postdata();
         $request['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
         $url = $req_req->get_normalized_http_url();
     } else {
         // notice GET, PUT and DELETE both needs to be passed in URL
         $url = $req_req->to_url();
     }
     $response = wp_remote_request($url, $request);
     $json_output = '';
     if (!is_wp_error($response)) {
         // make call
         $result = wp_remote_retrieve_body($response);
         $headers = wp_remote_retrieve_headers($response);
         $response_code = wp_remote_retrieve_response_code($response);
         // $json_output contains the output string
         $json_output = $result;
     } else {
         $response_code = $response->get_error_code();
     }
     remove_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
     remove_filter('http_request_timeout', array($this, 'filter_timeout_time'));
     // catch errors
     if (is_wp_error($response)) {
         throw new W3tcWpHttpException("ERROR: {$response->get_error_message()}, Output: {$json_output}", $response_code, null, $headers);
     }
     return $json_output;
 }
Пример #2
0
 /**
  * @param $selected_call
  * @param $method_type
  * @param $params
  * @return string
  * @throws CurlException
  */
 private function execute($selected_call, $method_type, $params)
 {
     $consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
     // the endpoint for your request
     $endpoint = "{$this->netdnarws_url}/{$this->alias}{$selected_call}";
     //parse endpoint before creating OAuth request
     $parsed = parse_url($endpoint);
     if (array_key_exists("parsed", $parsed)) {
         parse_str($parsed['query'], $params);
     }
     //generate a request from your consumer
     $req_req = W3tcOAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
     //sign your OAuth request using hmac_sha1
     $sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
     $req_req->sign_request($sig_method, $consumer, NULL);
     // create curl resource
     $ch = curl_init();
     // set url
     curl_setopt($ch, CURLOPT_URL, $req_req);
     //return the transfer as a string
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     // set curl custom request type if not standard
     if ($method_type != "GET" && $method_type != "POST") {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type);
     }
     if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") {
         $query_str = W3tcOAuthUtil::build_http_query($params);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str)));
         curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str);
     }
     // retrieve headers
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
     // make call
     $result = curl_exec($ch);
     $headers = curl_getinfo($ch);
     $curl_error = curl_error($ch);
     // close curl resource to free up system resources
     curl_close($ch);
     // $json_output contains the output string
     $json_output = substr($result, $headers['header_size']);
     // catch errors
     if (!empty($curl_error) || empty($json_output)) {
         throw new CurlException("CURL ERROR: {$curl_error}, Output: {$json_output}", $headers['http_code'], null, $headers);
     }
     return $json_output;
 }
Пример #3
0
 /**
  * Requests and returns the authorization tokens. To be used with get_authorize_url.
  * Exits application and prints message on WP_Error
  *
  * @return array
  */
 private function _get_auth_request_tokens()
 {
     $test_consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
     //prepare to get request token
     $sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
     $parsed = parse_url($this->request_token_url);
     $params = array('callback' => $this->base_url);
     if (isset($parsed['query'])) {
         parse_str($parsed['query'], $params);
     }
     $req_req = W3tcOAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", W3TC_CDN_OAUTH_REQUEST_TOKEN_URL, $params);
     $req_req->sign_request($sig_method, $test_consumer, NULL);
     $page = wp_remote_get($req_req->to_url());
     if (is_wp_error($page)) {
         $this->handle_error($page);
     }
     parse_str($page['body'], $tokens);
     return $tokens;
 }
Пример #4
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 = $parameters ? $parameters : array();
     $defaults = array("oauth_version" => W3tcOAuthRequest::$version, "oauth_nonce" => W3tcOAuthRequest::generate_nonce(), "oauth_timestamp" => W3tcOAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key);
     if ($token) {
         $defaults['oauth_token'] = $token->key;
     }
     $parameters = array_merge($defaults, $parameters);
     return new W3tcOAuthRequest($http_method, $http_url, $parameters);
 }