在PHP中使用HTTP基本身份验证发送HTTP post请求

在PHP中使用HTTP基本身份验证发送HTTP post请求,php,api,curl,basic-authentication,Php,Api,Curl,Basic Authentication,我有以下post api请求,提供的示例是使用cURL命令: curl "https://muserver.com/api/gettoken" --request POST --include - -header "Content-Type: application/json" --user test:test 我试图在PHP中使用cURL来执行此请求,但出现了身份验证错误 以下是我尝试过的: ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $u

我有以下post api请求,提供的示例是使用cURL命令:

curl "https://muserver.com/api/gettoken" --request POST --include -
-header "Content-Type: application/json" --user test:test
我试图在PHP中使用cURL来执行此请求,但出现了身份验证错误

以下是我尝试过的:

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);
我想我应该通过用户并通过。以

-用户测试:测试


但是真的不知道怎么做。

您似乎缺少$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);

它的可能副本不是副本。我已经查过那个帖子了。这里我需要通过-user test:test用户名是test,密码是test?是。。但是user是缺少curl资源变量的表名$
<?php


$url_simple_auth = 'https://www.some_url_need_basic_auth_first.com';
$url_download = 'https://www.some_url_need_basic_auth_first.com/dowonload_file.zip';
$username = 'some_login';
$password = 'some_pass';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_simple_auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// login simple auth
$simple_auth = curl_exec($ch);

// download file
curl_setopt($ch, CURLOPT_REFERER, $url_simple_auth);
curl_setopt($ch, CURLOPT_URL, $url_download);
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=$username&key=$password"); 
$result = curl_exec($ch);

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status_code);
var_dump($result);