首页 > 解决方案 > How to convert curl "-b" tag into php curl?

问题描述

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.

标签: phpcurl

解决方案


它已经被回答了数百次。不过以防万一,让我们在这里再复制一次,给那些找不到的人。

-b 表示“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);

推荐阅读