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

时间:2015-08-12 16:18:27

标签: php curl

我使用的API要求我发送一个带有来自文件的二进制数据的POST作为请求的主体。如何使用PHP cURL完成此操作?

命令行相当于我试图实现的目标:

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

5 个答案:

答案 0 :(得分:14)

您可以在result = df.sort_index(by=['ID Name'], ascending=[True]) 设置身体。

示例:

CURLOPT_POSTFIELDS

取自here

当然,设置自己的标题类型,只需为正文$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); 执行。

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

这可以通过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);

请参阅-https://github.com/andriichuk/php-curl-cookbook#upload-file

答案 3 :(得分:0)

将正文设置为二进制数据并在没有multipart/form-data的情况下上传,关键是欺骗卷发,首先我们告诉他PUT,然后告诉POST:

    <?php
    
    $file_local_full = '/tmp/foobar.png';
    $content_type = mime_content_type($file_local_full);

    $headers = array(
        "Content-Type: $content_type", // or whatever you want
    );

    $filesize = filesize($file_local_full);
    $stream = fopen($file_local_full, 'r');

    $curl_opts = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_PUT => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_INFILE => $stream,
        CURLOPT_INFILESIZE => $filesize,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
    );

    $curl = curl_init();
    curl_setopt_array($curl, $curl_opts);

    $response = curl_exec($curl);

    fclose($stream);

    if (curl_errno($curl)) {
        $error_msg = curl_error($curl);
        throw new \Exception($error_msg);
    }

    curl_close($curl);

积分:How to POST a large amount of data within PHP curl without memory overhead?

答案 4 :(得分:-1)

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

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

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

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