Jay Blanchard Jay Blanchard - 22 days ago 6
PHP Question

PHP cURL Access Fails to Remote Docker Hub/Registry

In order to facilitate a remote CICD process for our applications I am trying to access our Docker Hub/Registry to be able to list, tag and push images. Starting with the basics using the following from a bash script:

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)

I created some cURL in PHP, trying to get a token for 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");

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


The response:


If I enter the URL https://regsitry.hub.docker.com/v2/users/login/ directly into the address bar of the browser I get:

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

If I eliminate the username and password from the cURL request I still get the
response. There is very little (actually I found next to nothing) for using PHP to interact with the Docker Hub/Registry API(s). There are no errors in my error logs.

Am I missing something obvious? Or is there no way to interact with the Docker API via PHP?

Answer Source


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

is not equivalent to:

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

but this is:

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

and it works.