private function getOAuthHeader($location, $request) { try { $oauth = new OAuthSimple($this->getAccountId(), $this->getSharedKey()); $oauth->setAction("POST"); $oauth->genBodyHash($request); $parse = parse_url($location); $port = (isset($parse["port"]) and ($parse["port"] == '80' or $parse["port"] == '443')) ? '' : !isset($parse["port"]) ? '' : ':' . $parse["port"]; if (!is_null($this->language)) { $oauth->setParameters(array('lang' => $this->language)); } $oauth->setPath($parse["scheme"] . '://' . $parse["host"] . $port . $parse["path"]); $header_string = $oauth->getHeaderString(); $oauth->reset(); } catch (Exception $e) { throw new TurnitinSDKException('oautherror', $e->getMessage(), $this->getLogPath()); } return $header_string; }
private function createRequestContext($url, $method, &$content = null, $oauth_token = -1) { if ($oauth_token === -1) { $oauth_token = $this->accessToken; } $method = strtoupper($method); $http_context = array('method' => $method, 'header' => ''); $oauth = new OAuthSimple($this->consumerToken['t'], $this->consumerToken['s']); if (empty($oauth_token) && !empty($this->accessToken)) { $oauth_token = $this->accessToken; } if (!empty($oauth_token)) { $oauth->setParameters(array('oauth_token' => $oauth_token['t'])); $oauth->signatures(array('oauth_secret' => $oauth_token['s'])); } if (!empty($content)) { $post_vars = $method != "PUT" && preg_match("/^[a-z][a-z0-9_]*=/i", substr($content, 0, 32)); $http_context['header'] .= "Content-Length: " . strlen($content) . "\r\n"; $http_context['header'] .= "Content-Type: application/" . ($post_vars ? "x-www-form-urlencoded" : "octet-stream") . "\r\n"; $http_context['content'] =& $content; if ($method == "POST" && $post_vars) { $oauth->setParameters($content); } } elseif ($method == "POST") { // make sure that content-length is always set when post request (otherwise some wrappers fail!) $http_context['content'] = ""; $http_context['header'] .= "Content-Length: 0\r\n"; } // check for query vars in url and add them to oauth parameters (and remove from path) $path = $url; $query = strrchr($url, '?'); if (!empty($query)) { $oauth->setParameters(substr($query, 1)); $path = substr($url, 0, -strlen($query)); } $signed = $oauth->sign(array('action' => $method, 'path' => $path)); //print_r($signed); $http_context['header'] .= "Authorization: " . $signed['header'] . "\r\n"; return $this->useCurl ? $this->createCurl($url, $http_context) : stream_context_create(array('http' => $http_context)); }
/** * Обертка для отправки подписанного запроса через curl * * @param $url * @param string $method * @param array $data - POST данные * @param $opts - доп. параметры для curl * @return mixed */ public function doCurl($url, $method = "POST", $data = array(), $opts = array()) { $ch = curl_init($url); $opts += array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0); if ($method == "POST") { $opts[CURLOPT_POST] = TRUE; $opts[CURLOPT_POSTFIELDS] = http_build_query($data); } $oauth = new OAuthSimple($this->app_key, $this->app_secret); if (!$this->request_token && $this->token) { $this->request_token = $this->token; } if ($this->request_token) { $oauth->setParameters(array('oauth_token' => $this->request_token['oauth_token'])); $oauth->signatures(array('oauth_secret' => $this->request_token['oauth_token_secret'])); } if ($method == "POST" && count($data)) { $oauth->setParameters(http_build_query($data)); } $path = $url; $query = strrchr($url, '?'); if (!empty($query)) { $oauth->setParameters(substr($query, 1)); $path = substr($url, 0, -strlen($query)); } $signed = $oauth->sign(array('action' => $method, 'path' => $path)); $opts[CURLOPT_HTTPHEADER][] = "Authorization: " . $signed['header']; if ($method == "PUT") { $opts[CURLOPT_CUSTOMREQUEST] = "PUT"; } curl_setopt_array($ch, $opts); $result = curl_exec($ch); return $result; }
Defined in config.inc $apiKey $sharedSecret $accessToken $tokenSecret */ // Some sample argument values // You can pass in arguments either as a string of URL characters: $argumentsAsString = "term=mac%20and+me&expand=formats,synopsis&max_results=1"; // or a hash: $argumentsAsObject = array('term' => 'the prisoner', 'expand' => 'formats,synopsis', 'max_results' => '1', 'v' => '2.0', 'output' => 'json'); $path = "http://api.netflix.com/catalog/titles"; # Test 1 ==== $oauth = new OAuthSimple($apiKey, $sharedSecret); $oauth->setParameters($argumentsAsString); $oauth->setPath($path); $sample1Results = $oauth->sign(); # Test 2 ===== $oauth = null; $oauth = new OAuthSimple($apiKey, $sharedSecret); $sample2Results = $oauth->sign(array('action' => 'GET', 'path' => $path, 'parameters' => $argumentsAsObject)); # Test 3 ====== $oauth = new OAuthSimple(); $sample3Results = $oauth->sign(array('path' => 'http://api.netflix.com/catalog/people', 'parameters' => array('term' => 'Harrison Ford', 'max_results' => '5'), 'signatures' => array('consumer_key' => $apiKey, 'shared_secret' => $sharedSecret, 'access_token' => $accessToken, 'access_secret' => $tokenSecret))); ?> <html> <head> <title>Test Document</title> </head> <body>
function createRequestContext($url, $method, &$content, $oauth_token = -1) { if ($oauth_token === -1) { $oauth_token = $this->accessToken; } $http_context = array('method' => $method, 'header' => ''); $oauth = new OAuthSimple($this->consumerToken['t'], $this->consumerToken['s']); if (empty($oauth_token) && !empty($this->accessToken)) { $oauth_token = $this->accessToken; } if (!empty($oauth_token)) { $oauth->setParameters(array('oauth_token' => $oauth_token['t'])); $oauth->signatures(array('oauth_secret' => $oauth_token['s'])); } if (!empty($content)) { $post_vars = $method == "POST" && preg_match("/^[a-z][a-z0-9_]*=/i", substr($content, 0, 32)); $http_context['header'] .= "Content-Length: " . strlen($content) . "\r\n"; $http_context['header'] .= "Content-Type: application/" . ($post_vars ? "x-www-form-urlencoded" : "octet-stream") . "\r\n"; $http_context['content'] =& $content; if ($post_vars) { $oauth->setParameters($content); } } // check for query vars in url and add them to oauth parameters $query = strrchr($url, '?'); if (!empty($query)) { $oauth->setParameters(substr($query, 1)); } $signed = $oauth->sign(array('action' => $method, 'path' => $url)); $http_context['header'] .= "Authorization: " . $signed['header'] . "\r\n"; //echo "<br><br>SBS: $signed[sbs]<br><br>"; //print_r($http_context); return stream_context_create(array('http' => $http_context)); }