另一个失败的Twitter oAuth cURL访问令牌请求

另一个失败的Twitter oAuth cURL访问令牌请求,第1张

另一个失败的Twitter oAuth cURL访问令牌请求

以下是到目前为止我整理的内容,它有效:-)

    class Twitauth    {      var $key = '';      var $secret = '';      var $request_token = "https://twitter.com/oauth/request_token";    function Twitauth($config)    {        $this->key = $config['key']; // consumer key from twitter        $this->secret = $config['secret']; // secret from twitter    }    function getRequestToken()    {        // Default params        $params = array( "oauth_version" => "1.0", "oauth_nonce" => time(), "oauth_timestamp" => time(), "oauth_consumer_key" => $this->key, "oauth_signature_method" => "HMAC-SHA1"         );         // BUILD SIGNATURE // enpre params keys, values, join and then sort. $keys = $this->_urlenpre_rfc3986(array_keys($params)); $values = $this->_urlenpre_rfc3986(array_values($params)); $params = array_combine($keys, $values); uksort($params, 'strcmp'); // convert params to string  foreach ($params as $k => $v) {$pairs[] = $this->_urlenpre_rfc3986($k).'='.$this->_urlenpre_rfc3986($v);} $concatenatedParams = implode('&', $pairs); // form base string (first key) $baseString= "GET&".$this->_urlenpre_rfc3986($this->request_token)."&".$this->_urlenpre_rfc3986($concatenatedParams); // form secret (second key) $secret = $this->_urlenpre_rfc3986($this->secret)."&"; // make signature and append to params $params['oauth_signature'] = $this->_urlenpre_rfc3986(base64_enpre(hash_hmac('sha1', $baseString, $secret, TRUE)));         // BUILD URL // Resort uksort($params, 'strcmp'); // convert params to string  foreach ($params as $k => $v) {$urlPairs[] = $k."=".$v;} $concatenatedUrlParams = implode('&', $urlPairs); // form url $url = $this->request_token."?".$concatenatedUrlParams;         // Send to cURL         print $this->_http($url);   }    function _http($url, $post_data = null)    {    $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);        curl_setopt($ch, CURLOPT_TIMEOUT, 30);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        if(isset($post_data))        { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);        }        $response = curl_exec($ch);        $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);        $this->last_api_call = $url;        curl_close($ch);        return $response;    }    function _urlenpre_rfc3986($input)    {        if (is_array($input)) { return array_map(array('Twitauth', '_urlenpre_rfc3986'), $input);        }        else if (is_scalar($input)) { return str_replace('+',' ',str_replace('%7E', '~', rawurlenpre($input)));        }        else{ return '';        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5602476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存