为了方便我们的应用程序的远程CICD过程,我试图访问我们的Docker Hub / Registry,以便能够列出,标记和推送图像。从使用bash脚本中的以下内容的the basics开始:
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 Hub的令牌:
$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);
回复:
布尔(假)
如果我将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进行交互的事实很少(实际上我几乎找不到)。我的错误日志中没有错误。
我错过了一些明显的东西吗?或者没有办法通过PHP与Docker API交互?
答案 0 :(得分:1)
此:
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
不等同于:
-d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}'
但这是:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username'=>$user, 'password'=>$pass]));
它有效。