2017-09-16 189 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);