1

I try to convert this curl command (proxmox api) into php curl : curl -k -b "PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..." https://10.0.0.1:8006/api2/json/

I used the site https://incarnate.github.io/curl-to-php/ to convert it but it doesn't support the "-b" tag.

After some search, I tried this code :

<?php

$apiurlvmName = "https://10.0.0.1:8006/api2/json/";
$proxmoxid = "username=root@pam&password=mypass";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiurlvmName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $proxmoxid);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
else {
    echo $result;
}

curl_close($ch);

?>

But it doesn't works. The page takes few seconds to load then I get a blank page without error displayed. As if "curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");" were not enough.

Did I missed something ? How can I convert the curl "-b" tag correctly into php ? Thank you.

Robg
  • 29
  • 6
  • This question should be marked as dublicate, solution already exists please see https://stackoverflow.com/questions/16872082/how-can-i-send-cookies-using-php-curl-in-addition-to-curlopt-cookiefile – Ersin Demirtas Feb 13 '20 at 18:14
  • You can use https://curlconverter.com/php/ which supports `-b`. – Boris Verkhovskiy Sep 20 '22 at 05:32

1 Answers1

0

It has already been answered hundreds of times. But just in case, let's copy it here again for those who can't find it.

-b means "COOKIE".

# sending manually set cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..."));

# sending cookies from file
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
Fazıl Akbulut
  • 198
  • 2
  • 14