Post二进制从变量与纯文本一起使用curl与php


post binary from variable along with plain text using curl with php

我知道只发布二进制:

$file_contents = file_get_contents("http://example.org/image.jpg");
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_contents); 

以及从磁盘上的文件发布文本和二进制文件:

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
  "name" => "peter",
  "msg" => "hello",
  "foo" => "@file/on/disk/image.jpg"
));

但是我无法弄清楚的是如何直接从变量(例如$file_contents)以及纯文本键值对发布二进制数据。旋度有可能吗?

我问的原因是,我正在使用facebook API来创建事件,并需要沿着事件数据的其余部分上传图像。因为我从互联网上得到的图像,这将是伟大的,如果我没有在发布之前保存在磁盘上,然后从磁盘中删除它们,而是直接做从file_get_contents()保存在一个php变量的数据的帖子。

您可以在url中以querystring的形式传递参数,并在post字段中传递二进制数据。在接收端,所有参数都可以从$_REQUEST

中检索。
$file_contents = file_get_contents("http://example.org/image.jpg");
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://example.com/test.aspx?name=peter&msg=hello');
curl_setopt($c, CURLOPT_POSTFIELDS, $file_contents);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_VERBOSE, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 0);
$cResponse = curl_exec($c);
curl_close($c);