/** * @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; }
public static function build_http_query($params) { if (!$params) { return ''; } // Urlencode both keys and values $keys = W3tcOAuthUtil::urlencode_rfc3986(array_keys($params)); $values = W3tcOAuthUtil::urlencode_rfc3986(array_values($params)); $params = array_combine($keys, $values); // Parameters are sorted by name, using lexicographical byte value ordering. // Ref: Spec: 9.1.1 (1) uksort($params, 'strcmp'); $pairs = array(); foreach ($params as $parameter => $value) { if (is_array($value)) { // If two or more parameters share the same name, they are sorted by their value // Ref: Spec: 9.1.1 (1) // June 12th, 2010 - changed to sort because of issue 164 by hidetaka sort($value, SORT_STRING); foreach ($value as $duplicate_value) { $pairs[] = $parameter . '=' . $duplicate_value; } } else { $pairs[] = $parameter . '=' . $value; } } // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61) // Each name-value pair is separated by an '&' character (ASCII code 38) return implode('&', $pairs); }