2017-09-16 234 views
0
的HTTP基本驗證

我有以下職位API請求,所提供的例子是使用cURL命令:發送HTTP POST請求與PHP

curl "https://muserver.com/api/gettoken" --request POST --include - 
-header "Content-Type: application/json" --user test:test 

我試圖用捲曲在PHP,但得到驗證執行此要求錯誤。

以下是我曾嘗試:

ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

我想我應該通過用戶和通過。在

--user測試形式:測試

,但真的不知道該怎麼做。

+0

的可能的複製[?我如何使用HTTP基本身份驗證使用PHP捲曲的請求(https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – Dekel

+0

它不重複。我已經檢查過這個帖子。這裏我需要通過--user test:test – Fshamri

+0

用戶名是'test',密碼是'test'? – Dekel

回答

0

你似乎缺少$ username和$ password變量:

$username='test'; 
$password='test'; 

ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch);