2011-10-24 61 views
1

我真的需要你的幫助。使用php的twitter授權Http_request

我試圖從php獲取請求令牌,而不使用cURL。

我的第一次嘗試是使用fsockopen。

$fp = fsockopen("ssl://api.twitter.com", 443) or die ("unable to open socket"); 

然後,我做了HTTP POST標題如下。

$auth = "oauth_callback="http%3A%2F%2Fmydomain.org%2F~myID%2Ftwt-oauth%2Fcallback.php", oauth_consumer_key="ExmfrhvgMkUmwzGKCg5FQw", oauth_nonce="da4dfa747b27d83c9bbda69452a87bee", oauth_signature="L9ucYZUfKQ%2BRkp1FQNbzEXXrl1w%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1319451330", oauth_version="1.0" "; 


$msg = "POST /oauth/request_token HTTP/1.1 \r\n"; 
$msg .= "Host: api.twitter.com \r\n"; 
$msg .= "Authorization: Oauth " . $auth . "\r\n"; 
$msg .= "Connection:Keep-Alive \r\n\r\n"; 
$bytes = fwrite($fp, $msg); 

結果是

HTTP/1.1 401 Unauthorized 
Date: Mon, 24 Oct 2011 10:15:30 GMT 
Server: hi 
Status: 401 Unauthorized 
X-Transaction: 1319451330-75511-27215 
X-Frame-Options: SAMEORIGIN 
Last-Modified: Mon, 24 Oct 2011 10:15:30 GMT 
X-Runtime: 0.00602 
Content-Type: text/html; charset=utf-8 
Content-Length: 44 
Pragma: no-cache 
X-Content-Type-Options: nosniff 
X-Revision: DEV 
Expires: Tue, 31 Mar 1981 05:00:00 GMT 
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 
X-MID: 6301c02e82f43071b60e91cc31326d5354818c83 
Set-Cookie: k=143.248.234.102.1319451330738600; path=/; expires=Mon, 31-Oct-11 10:15:30 GMT; domain=.twitter.com 
Set-Cookie: guest_id=v1%3A131945133074814112; domain=.twitter.com; path=/; expires=Wed, 23-Oct-2013 22:15:30 GMT 
Set-Cookie: _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCL2IbTUzAToHaWQiJWEyZDlhNjMzYWJmMmNh%250ANmE2NDU1OTMyMzQ2MWY4MTkxIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--f5d5dc205f414bd74c0270e9610712ad1e04d2ee; domain=.twitter.com; path=/; HttpOnly 
Vary: Accept-Encoding 
Keep-Alive: timeout=15, max=100 
Connection: Keep-Alive 

Failed to validate oauth signature and token 

我不知道爲什麼......我是否需要BASE64_ENCODE或進行urlencode $權威性? (那麼,如何?)
上面的$ auth的值是我從日誌中複製和粘貼的內容。 (是否有可能有問題與「」?)

我試過Http_request很簡單,然後。

$httpReq = new HTTP_Request("https://api.twitter.com/oauth/request_token"); 
$httpReq->setMethod(HTTP_REQUEST_METHOD_POST); 
$httpReq->addHeader("Authorization", $auth); 
$res = $httpReq->sendRequest(); 

然後,$res->getMessage()爲「失敗驗證的OAuth簽名和令牌」 一樣的第一次嘗試的上方。

有什麼問題?

我在這裏停留相當長的時間......真正需要幫助的..

我加入的捲曲使用的版本代碼..我想它的相同的結果...

function http($url, $method, $postfields = NULL) { 

// 
    $this->http_info = array(); 
    $ci = curl_init(); 
    // Curl settings 
    curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); 
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); 
    curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); 
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); 
    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); 
    curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); 
    curl_setopt($ci, CURLOPT_HEADER, FALSE); 

    switch ($method) { 
     case 'POST': 
     curl_setopt($ci, CURLOPT_POST, TRUE); 
     if (!empty($postfields)) { 
      curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); 
     }  
     break; 
     case 'DELETE': 
     curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
     if (!empty($postfields)) { 
      $url = "{$url}?{$postfields}"; 
     }  
    }  

    curl_setopt($ci, CURLOPT_URL, $url); 
    $response = curl_exec($ci); 
    $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); 
    $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); 
    $this->url = $url; 
    curl_close ($ci); 
    // 

    return $response; 
    } 

    /** 
    * Get the header info to store. 
    */ 
    function getHeader($ch, $header) { 
    $i = strpos($header, ':'); 
    if (!empty($i)) { 
     $key = str_replace('-', '_', strtolower(substr($header, 0, $i))); 
     $value = trim(substr($header, $i + 2)); 
     $this->http_header[$key] = $value; 
    }  
    return strlen($header); 
    } 

回答

1

看看你是否使用my HTTP request class,它使用fsockopen() - 它是徹底的嘗試和測試(由我)。目前不支持Oauth本地支持,但您應該能夠使用HTTPRequest::setRequestHeader()手動設置標題。如果您安裝了本機HTTPRequest類,則可能需要重命名該類 - 我最初編寫它用於PHP4環境,其中我沒有流支持並且無法控制已安裝的擴展。

記錄在文件頂部的註釋中。

+0

嗨戴夫,感謝您的幫助。我突然發現問題在於加密oauth ......其中一個參數是錯誤的......你的課程可能對我有用。再次感謝! :) – kong