PHP : Sending Boundary Values in Curl Headers

标签 php curl

我想在 curl header 中发送以下数据。

Content-Type: multipart/form-data; boundary=---------------------------28854217951032752725060171

我使用了下面的代码,但似乎不起作用
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data; boundary=---------------------------28854217951032752725060171'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch); 
$last = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$info = curl_getinfo($ch);
return array($result,$last,$info);
curl_close($ch);

发送边界值的正确方法是什么。
P.s : 28854217951032752725060171 是常数值。

最佳答案

您无需手动发送此值。 Curl 会自己处理这个。

编辑:
CURLOPT_POSTFIELDS可以接受数组,它会附加 Content-Type以及自动需要的边界。这是quote来自 PHP 文档:

If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.



例如:
$data = array(
    'name' => 'Foo',
    'file' => '@/absolute/path/to/some/file'
);

关于PHP : Sending Boundary Values in Curl Headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15183725/

相关文章:

php - 图像替换

firefox - 如何使用curl 通过代理模拟来自Firefox 的请求?

linux - 在 Linux 上为 Curl 添加对 SCP 和 SFTP 的支持

curl - 使用 CURL 删除 FTP 目录内容

php - 从 WordPress 自定义表中删除行

php - 在类中定义变量并在函数中使用它

php - .htaccess 错误文档重定向

php - 长轮询超出最大执行时间

python - 如何在 Python 中执行 Curl

php - 如何使用node.js获取SSL证书信息?