2011-05-16 171 views
6

我試圖使用PHP連接到Webtrend的API,但一直未能進行身份驗證。Webtrends API身份驗證PHP

的WT的文檔上給出的例子是.NET或Ruby是,.NET的例子是這樣的:

var svc = new WebClient(); 
     svc.Credentials = new NetworkCredential("yourWebTrendsAccount\WebTrendsUserName", "yourSuperSecretPassword"); 
     svc.DownloadStringCompleted += svc_DownloadStringCompleted; 
     svc.DownloadStringAsync(new Uri(baseUri)); 

我不熟悉.NET,但有沒有WebClient類的等效在PHP上?

我一直在嘗試使用

username = "my_account_name/my_login_name" 
password = "my_password" 

但至今沒有運氣使用curl進行身份驗證。我收到一條錯誤消息,指出參數不正確。

更新:添加代碼

$username=urlencode('my_account_name\my_login_name'); 
    $password="my_password"; 


    $postdata="username=$username&password=$password"; 

    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL,"https://ws.webtrends.com/v2/ReportService/profiles/XXXXXXXX/reports/XXXXXXXX/?totals=all&period=2011w14&format=xml"); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
    curl_setopt ($ch, CURLOPT_POST, 1); 
    $result = curl_exec ($ch); 
    curl_close($ch); 
    var_dump($result); 

我也試過

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

,但至今沒有運氣。

+0

你的PHP腳本 – Ibu 2011-05-16 07:54:56

+0

的展示細節我解決了這一問題。 在查看http頭中的錯誤消息('SSL證書問題,驗證CA證書是否正確')後,我將這些行添加到代碼中,現在它可以工作: – Jon 2011-05-16 14:41:12

+0

curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0); – Jon 2011-05-16 14:41:25

回答

0

@Kevin霍斯特提供執行與捲曲和PHP基本的認證請求的一個很好的例子。我經常需要通過命令行來做到這一點。要做到這一點,你需要在你的系統上安裝curl。

curl --user username:password \ 
-i https://ws.webtrends.com/v2/ReportService/profiles/XXXXXXXX/reports/XXXXXXXX/?totals=all&period=2011w14&format=xml 

根據WebTrends Data Extraction API的文檔。他們使用基於SSL的基本身份驗證,這是RESTful身份驗證的標準。

WebTrends Data extraction API

1

我認爲你必須使用CURLOPT_USERPWD代替POSTDATA的:

$connection = curl_init(); 
curl_setopt($connection, CURLOPT_URL, 'https://ws.webtrends.com/v2/ReportService/profiles/XXXXXXXX/reports/XXXXXXXX/?totals=all&period=2011w14&format=xml'); 
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt(CURLOPT_USERPWD, sprinf('%s:%s', $username, $password)) 
$data = curl_exec(); 
curl_close($ch);