PHP cURL:如何将正文设置为二进制数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31970541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:44:47  来源:igfitidea点击:

PHP cURL: how to set body to binary data?

phpcurl

提问by Nocklas

I'm using an API that wants me to send a POST with the binary data from a file as the body of the request. How can I accomplish this using PHP cURL?

我正在使用一个 API,它希望我发送一个 POST,其中包含来自文件的二进制数据作为请求的正文。如何使用 PHP cURL 完成此操作?

The command line equivalent of what I'm trying to achieve is:

相当于我想要实现的命令行是:

curl --request POST --data-binary "@myimage.jpg" https://myapiurl

回答by vfsoraki

You can just set your body in CURLOPT_POSTFIELDS.

你可以把你的身体放在CURLOPT_POSTFIELDS.

Example:

例子:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     "body goes here" ); 
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain')); 

$result=curl_exec ($ch);

Taken from here

取自这里

Of course, set your own header type, and just do file_get_contents('/path/to/file')for body.

当然,设置你自己的header类型,只file_get_contents('/path/to/file')为body做。

回答by Serhii Andriichuk

This can be done through CURLFile instance:

这可以通过 CURLFile 实例完成:

$uploadFilePath = __DIR__ . '/resource/file.txt';

if (!file_exists($uploadFilePath)) {
    throw new Exception('File not found: ' . $uploadFilePath);
}

$uploadFileMimeType = mime_content_type($uploadFilePath);
$uploadFilePostKey = 'file';

$uploadFile = new CURLFile(
    $uploadFilePath,
    $uploadFileMimeType,
    $uploadFilePostKey
);

$curlHandler = curl_init();

curl_setopt_array($curlHandler, [
    CURLOPT_URL => 'https://postman-echo.com/post',
    CURLOPT_RETURNTRANSFER => true,

    /**
     * Specify POST method
     */
    CURLOPT_POST => true,

    /**
     * Specify array of form fields
     */
    CURLOPT_POSTFIELDS => [
        $uploadFilePostKey => $uploadFile,
    ],
]);

$response = curl_exec($curlHandler);

curl_close($curlHandler);

echo($response);

See - https://github.com/andriichuk/php-curl-cookbook#upload-file

见 - https://github.com/andriichuk/php-curl-cookbook#upload-file

回答by heriipurnama

Try this:

尝试这个:

$postfields = array(
    'upload_file' => '@'.$tmpFile
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'/instances');
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);//require php 5.6^
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);

if (curl_errno($ch)) {
    print curl_error($ch);
}
curl_close($ch); 

回答by Samar

You need to provide appropriate header to send a POST with the binary data.

您需要提供适当的标头以发送带有二进制数据的 POST。

$header = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_POSTFIELDS, $arr_containing_file);

Your $arr_containing_file can for example contain file as expected (I mean, you need to provide appropriate expected field by the API service).

例如,您的 $arr_ contains_file 可以包含预期的文件(我的意思是,您需要通过 API 服务提供适当的预期字段)。

$arr_containing_file = array('datafile' => '@inputfile.ext');