2015-12-07 101 views
0

我試圖讓我的訪問令牌在Twitter的API: https://dev.twitter.com/oauth/application-only捲曲後頭球PHP

我在使用PHP捲曲,我沒有得到的結果!

我的代碼:

$consumer_key  = "XXXXXXXX"; 
$consumer_secret = "XXXXXXXX"; 

$base64    = base64_encode($consumer_key.':'.$consumer_secret); 

$data = array("grant_type" => "client_credentials"); 

$header = array(
       "Authorization"  => "Basic $base64", 
       "Content-Type"  => "application/x-www-form-urlencoded;charset=UTF-8", 
       "Accept-Encoding" => "gzip", 
      ); 

$curlObj = curl_init(); 
curl_setopt($curlObj, CURLOPT_URL, 'https://api.twitter.com/oauth2/token'); 
curl_setopt ($curlObj, CURLOPT_HTTPHEADER, $header); 
curl_setopt($curlObj, CURLOPT_POST, 1); 
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $data); 
$response = curl_exec($curlObj); 
$json = json_decode($response, true); 
curl_close($curlObj); 

print_r($json); 

問題出在哪裏?

+0

你只是假設成功。 curl_exec在失敗時返回布爾值false。 'if($ response === false){die(curl_error($ curlObj)); }' –

回答

0

試用curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);

+0

我嘗試它但不工作:( – user3492381

0

您發送的郵件標題不正確。標題數組不在鍵值對中。這只是一個簡單的數組。如下圖所示:

$header = array(
    "Authorization: Basic $base64", 
    "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", 
    "Accept-Encoding: gzip", 
); 

而對於後場使用過$data功能http_build_query

curl_setopt($curlObj, CURLOPT_POSTFIELDS, http_build_query($data)); 
+0

先生Sabuj,不工作:(我得到空的頁面。 – user3492381