2015-01-12 92 views
1

我使用Yahoo API創建了一個新應用程序。我如何使用CURL功能傳遞所需的標題?我得到這個錯誤消息時,我想:將OAuth令牌傳遞給Yahoo API

<yahoo:error xml:lang="en-US"><yahoo:description>Please provide valid credentials. OAuth oauth_problem="unable_to_determine_oauth_type", realm="yahooapis.com"</yahoo:description></yahoo:error> 

如何傳遞所需的頭部在此代碼:

$url ="http://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1"; 
$ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_URL, $url); 
//get the url contents 
$data = curl_exec($ch);  
//execute curl request curl_close($ch); 
$xml = simplexml_load_string($data);  
print_r($xml);  
exit;   
+0

請發表您的代碼 – samgak

+0

我們如何在這裏傳遞標題。 $ url =「http://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1」; $ ch = curl_init(); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ ch,CURLOPT_URL,$ url); //獲取url內容 $ data = curl_exec($ ch); //執行卷曲請求 curl_close($ ch); $ xml = simplexml_load_string($ data); print_r($ xml); exit; – xyz

+0

請[編輯](http://stackoverflow.com/posts/27903093/edit)並添加註釋中的代碼到您的問題 – SearchAndResQ

回答

1

一旦你獲得一個OAuth 2.0訪問令牌,使用它在以下代碼:

$token = "<token>"; 

$url = "https://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1?format=json"; 

$headers = array(
    'Authorization: Bearer ' . $token, 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$response = curl_exec($ch); 
curl_close($ch); 
$json = json_decode($response); 
print_r($json); 

注意,它提出了使用https URL方案,並請求返回內容的安全傳輸通道的Authorization HTTP報頭中的令牌作爲JSON使用format URL查詢參數。

+0

誰能告訴如何從消費者密鑰和祕密 – xyz

+0

創建令牌它在這裏拼寫: https://developer.yahoo.com/oauth2/guide/ –

+0

謝謝。它有幫助 – xyz

相關問題