![](/img/trans.png)
[英]Using gikom openfire REST API when hit using the php code , the error appears as 401 unauthorized
[英]I tried to call a post request using PHP code, but I got 401 Unauthorized error.
我嘗試使用此PHP代碼發送發布請求,但拋出401未經授權錯誤:
$username = 'MyDomain\testuser';
$password = '123456';
$url = 'http://10.20.30.40:8080/TargetPage.aspx';
$data = array(
'username' => $username,
'password' => $password,
'postdata' => 'InputParameter1=Test1&InputParameter2=Test2'
);
$options = array(
'http' => array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
您沒有執行適當的基本身份驗證。 您必須自己構建authenticate標頭:
$options = array(
'http' => array(
'header' => 'Authorization: Basic ' . base64_encode("$username:$password")
)
);
您不能只是將用戶名/密碼元素放入數組中並期望它能工作。 PHP不知道您正在構建到HTTP基本身份驗證的流……您必須自己提供一切。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.