2014-04-18 52 views
0

我試圖連接到Twitter api服務器以創建「僅應用程序認證」。 我不在乎任何其他方式連接到Twitter。我需要這個特定的方法。通過代理服務器的SSL連接

我需要通過我公司的代理本地主機去api.twitter.com這就需要SSL

在此之後的Twitter開發者的https://dev.twitter.com/docs/auth/application-only-auth頁面的指示,我試着用:

捲曲:

try { 
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url); 
    if ($this->proxy != '') { 
     curl_setopt($ch, CURLOPT_PROXY, $this->proxy); 
     curl_setopt($ch, CURLOPT_PROXYPORT, $this->port); 
     curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->userpwd); 
    } 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(" 
     POST /oauth2/token HTTP/1.1 
     Host: api.twitter.com 
     User-Agent: My Twitter App v1.0.23 
     Authorization: Basic ".base64_encode(urlencode($consumer_key).":".urlencode($consumer_secret))." 
     Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
     Content-Length: 29 
     Accept-Encoding: gzip 
     grant_type=client_credentials 
    ")); 
    $response = curl_exec($ch); 
    if (FALSE === $response) throw new Exception(curl_error($ch), curl_errno($ch)); 
     curl_close($ch); 
     var_dump(json_decode($response)); 
} 
catch(Exception $e) { 
    trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR); 
} 

這給了我

Fatal error: Curl failed with error #35: Unknown SSL protocol error in connection to api.twitter.com

周的file_get_contents:

$context = stream_context_create(array(
    "http" => array(
    "method"=>"CONNECT", 
    "proxy" => $this->proxy.":".$this->port, 
    "request_fulluri" => true, 
    "header" => " 
     POST /oauth2/token HTTP/1.1 
     Host: api.twitter.com 
     User-Agent: My Twitter App v1.0.23 
     Proxy-Authorization: Basic ".base64_encode(urlencode($this->userpwd))." 
     Authorization: Basic ".base64_encode(urlencode($consumer_key).":".urlencode($consumer_secret))." 
     Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
     Content-Length: 29 
     Accept-Encoding: gzip 
     grant_type=client_credentials 
     ", 
    ), 
)); 
$response = file_get_contents($url, False, $context); 
var_dump(json_decode($response)); 

這給了我

Warning: file_get_contents(https://api.twitter.com/oauth2/token) [function.file-get-contents]: failed to open stream: Cannot connect to HTTPS server through proxy

的fsockopen:

$fp = fsockopen($this->proxy, $this->port); 
fputs($fp, " 
    POST /oauth2/token HTTP/1.1 
    Host: api.twitter.com 
    User-Agent: My Twitter App v1.0.23 
    Authorization: Basic ".base64_encode(urlencode($consumer_key).":".urlencode($consumer_secret))." 
    Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
    Content-Length: 29 
    Accept-Encoding: gzip 
    grant_type=client_credentials 
"); 
$data=""; 
while (!feof($fp)) $data .= fgets($fp,1024); 
fclose($fp); 
var_dump($data); 

這給了我

HTTP/1.1 400 Bad Request 
Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: text/html; charset=utf-8 
Proxy-Connection: close 
Connection: close 
Content-Length: 727 

我敢肯定,443端口是開放的,它的不是祿的問題alhost(我在嘗試在線服務器時遇到了同樣的錯誤)。

我甚至嘗試使用CONNECT方法代替POST。 我嘗試了隧道代理,但我不確定我是否做到了這一點,也沒有這個問題。

我跑出來的想法..

回答

0

發現這個問題。沒有必要(也許只有在這種情況下,我不確定)base64編碼的憑據。他們將被服務器編碼。

我不知道那個不同的錯誤響應的原因,但實際上是雙重編碼的問題,因爲服務器無法驗證我的憑證。

1

嘗試刪除這個:

curl_setopt($ch, CURLOPT_SSLVERSION, 3); 

你只有這個陣列中的一個值,這是錯的。

curl_setopt($ch, CURLOPT_HTTPHEADER, array(" 
     POST /oauth2/token HTTP/1.1 
     Host: api.twitter.com 
     User-Agent: My Twitter App v1.0.23 
     Authorization: Basic ".base64_encode(urlencode($consumer_key).":".urlencode($consumer_secret))." 
     Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
     Content-Length: 29 
     Accept-Encoding: gzip 
     grant_type=client_credentials 
    ")); 

更改上述本:

$consumer_key = base64_encode(urlencode($consumer_key); 
$consumer_secret = urlencode($consumer_secret); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "Host: api.twitter.com", 
     "User-Agent: My Twitter App v1.0.23", 
     "Authorization: Basic $consumer_key:$consumer_secret", 
     "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", 
     "Accept-Encoding: gzip", 
     "grant_type=client_credentials" 
     )); 
如果要包括 Content-Length: xx,你需要使用 strlen()獲得那個職位,前的字符串長度

;

$length = strlen($post_content); 

然後將它添加到CURLOPT_HTTPHEADER數組:

"Content-Length: $length" 
+0

謝謝你,但不幸的是,你的回答沒有幫助.. –

相關問題