PHP的cURL访问无法远程Docker中心/registry
为了方便我们的应用程序的远程CICD过程,我试图访问我们的Docker Hub / Registry,以便能够列出,标记和推送图像。 从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中创build了一些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);
响应:
布尔(假)
如果我直接在浏览器的地址栏中inputURL 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)进行交互的情况非常less( 实际上我几乎找不到 )。 我的错误日志中没有错误。
我错过了什么明显的? 还是有没有办法通过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]));
它的工作。