PHP cURL 访问远程失败 Docker Hub/Registry

PHP cURL Access Fails to Remote Docker Hub/Registry

为了促进我们应用程序的远程 CICD 进程,我正在尝试访问我们的 Docker Hub/Registry 以便能够列出、标记和推送图像。从 the basics 开始,使用 bash 脚本中的以下内容:

TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)

我在 PHP 中创建了一些 cURL,试图为 Docker 中心获取令牌:

$user = 'xxxxxxxx';
$pass = 'xxxxxxxx';
$namespace = 'xxxxxxxx';
$headers = array();
$headers[] = 'Content-Type: application/json';

$url = "https://hub.docker.com/v2/users/login/";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$return = curl_exec($ch);
curl_close ($ch);

var_dump($return);

回复:

bool(false)

如果我直接在浏览器的地址栏中输入 URL https://regsitry.hub.docker.com/v2/users/login/ 我会得到:

{"username": ["This field is required."], "password": ["This field is required."]}

如果我从 cURL 请求中删除用户名和密码,我仍然会得到 bool(false) 响应。使用 PHP 与 Docker Hub/Registry API(s ).我的错误日志中没有错误。

我是不是遗漏了什么明显的东西?或者没有办法通过 PHP 与 Docker API 互动?

这个:

curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");

不等同于:

-d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}'

但这是:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username'=>$user, 'password'=>$pass]));

而且有效。