Set the signatures (as well as validate the ones you have)
public signatures ( $signatures ) : OAuthSimple | ||
$signatures | (object) object/hash of the token/signature pairs {api_key:, shared_secret:, oauth_token: oauth_secret:} | |
return | OAuthSimple |
/** * Обертка для отправки подписанного запроса через 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; }
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)); }
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)); }