【问题标题】:How to convert curl "-b" tag into php curl?如何将 curl“-b”标签转换为 php curl?
【发布时间】:2020-05-29 10:30:45
【问题描述】:

我尝试将此 curl 命令(proxmox api)转换为 php curl:curl -k -b "PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..." https://10.0.0.1:8006/api2/json/

我使用网站https://incarnate.github.io/curl-to-php/进行转换,但它不支持“-b”标签。

经过一番搜索,我尝试了这段代码:

<?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);

?>

但它不起作用。该页面需要几秒钟才能加载,然后我得到一个没有显示错误的空白页面。好像"curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root@pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");" 还不够。

我错过了什么吗?如何将 curl "-b" 标签正确转换为 php ?谢谢。

【问题讨论】:

标签: php curl


【解决方案1】:

它已经被回答了数百次。不过以防万一,还是把它复制到这里给找不到的人吧。

-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);

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2021-03-09
    • 2017-06-05
    • 2017-08-03
    • 2019-06-04
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多