WebTrends API身份验证PHP
题
我试图使用php连接到webtrend的api,但尚未能够进行身份验证。
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,但是php上的webclient类是否有相同的webclient类?
我一直在尝试使用使用curl进行身份验证
username = "my_account_name/my_login_name"
password = "my_password"
.
但到目前为止没有运气。我收到错误消息,说参数不正确。
更新:添加代码
.
$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");
但到目前为止没有运气。
解决方案
@kevin horst提供了使用curl和php执行基本身份验证请求的一个很好的例子。我经常需要通过命令行执行此操作。为此,您需要在系统上安装卷曲。
curl --user username:password \
-i https://ws.webtrends.com/v2/ReportService/profiles/XXXXXXXX/reports/XXXXXXXX/?totals=all&period=2011w14&format=xml
.
根据WebTrends数据提取API的文档。它们使用SSL的基本身份验证,这是RESTful身份验证的标准。
其他提示
我认为你必须使用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);
.