php - 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.
解决方案
它已经被回答了数百次。不过以防万一,让我们在这里再复制一次,给那些找不到的人。
-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);
推荐阅读
- .net-core - 创建一个包含 3 个表中的列的视图
- typescript - 如何在 eslint 中使用 atom-typescript 或替代方法
- charts - ApexCharts:隐藏图表中的每第 n 个标签
- android - 如何修复“未安装应用程序” - Xamarin 应用程序分发
- sql - 统计多张表的普通用户数
- javascript - 如何删除多个数组项
- linux - 如何删除与字符串中的项目相似的文件
- c# - Google 请求同步操作返回 403
- git - 如何更改 PC 上的 Fork 文件位置
- mvvm - UWP ObservableCollection 通知 View 发生变化