简体   繁体   English

卷曲不在PHP中发布文件

[英]Curl not posting file in PHP

I am using the Brightcove API to upload video files from my server to my Brightcove account. 我正在使用Brightcove API将视频文件从我的服务器上传到我的Brightcove帐户。 I had it working with the following code: 我使用了以下代码:

$fields = array(
    'json' => json_encode( array(
        'method' => "create_video",
        'params' => array(
            'video' => array(
                'name' => $video->submission_by_name.' '.time(),
                'shortDescription' => $video->submission_question_1
            ),
            "token" => $this->config->item('brightcove_write_token'),
            "encode_to" => "MP4",
            "create_multiple_renditions" => "True"
        ))
    ),
    'file' => new CURLFile(FCPATH.'assets/uploads/submitted/'.$video->filename)
);

//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $this->config->item('brightcove_write_endpoint'));
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = json_decode(curl_exec($ch));

This was working locally however the live server is running PHP 5.3 so I can't use new CURLFile() 这是在本地工作但是实时服务器运行PHP 5.3所以我不能使用new CURLFile()

How would I achieve sending the file in a way that will work with PHP 5.3.3? 如何以与PHP 5.3.3一起使用的方式发送文件? I tried changing the file filed to use the @ syntax instead like so: 我尝试更改文件字段以使用@语法,如下所示:

'file' => '@' . FCPATH.'assets/uploads/submitted/'.$video->filename

but that doesn't seem to work, the API returns an error saying: 但这似乎不起作用,API返回错误说:

FilestreamRequiredError: upload requires a multipart/form-data POST with a valid filestream

So it looks like the file isn't being posted through. 所以看起来文件没有通过发布。

I've also tried copying the curl_file_create function like so: 我也试过像这样复制curl_file_create函数:

private function custom_curl_file_create($filename, $mimetype = '', $postname = '')
{
    if($mimetype=='') {
        $mimetype = mime_content_type($filename);
    }
    return "@$filename;filename="
        . ($postname ?: basename($filename))
        . ($mimetype ? ";type=$mimetype" : '');
}

And then doing: 然后做:

'file' => $this->custom_curl_file_create(FCPATH.'assets/uploads/submitted/'.$video->filename)

This doesn't work either though, the API returns the same error as before 但这不起作用,API返回与以前相同的错误

There are "@" issue on multipart POST requests apparently. 显然有多个POST请求有“@”问题。 I found the following information which solved the problem: 我找到了解决问题的以下信息:

Solution for PHP 5.5 or later:
- Enable CURLOPT_SAFE_UPLOAD.
- Use CURLFile instead of "@".

Solution for PHP 5.4 or earlier:
- Build up multipart content body by youself.
- Change "Content-Type" header by yourself.

The following snippet will help you :D

<?php

/**
* For safe multipart POST request for PHP5.3 ~ PHP 5.4.
* 
* @param resource $ch cURL resource
* @param array $assoc "name => value"
* @param array $files "name => path"
* @return bool
*/
function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) {

    // invalid characters for "name" and "filename"
    static $disallow = array("\0", "\"", "\r", "\n");

    // build normal parameters
    foreach ($assoc as $k => $v) {
        $k = str_replace($disallow, "_", $k);
        $body[] = implode("\r\n", array(
            "Content-Disposition: form-data; name=\"{$k}\"",
            "",
            filter_var($v), 
        ));
    }

    // build file parameters
    foreach ($files as $k => $v) {
        switch (true) {
            case false === $v = realpath(filter_var($v)):
            case !is_file($v):
            case !is_readable($v):
                continue; // or return false, throw new InvalidArgumentException
        }
        $data = file_get_contents($v);
        $v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v));
        $k = str_replace($disallow, "_", $k);
        $v = str_replace($disallow, "_", $v);
        $body[] = implode("\r\n", array(
            "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"",
            "Content-Type: application/octet-stream",
            "",
            $data, 
        ));
    }

    // generate safe boundary 
    do {
        $boundary = "---------------------" . md5(mt_rand() . microtime());
    } while (preg_grep("/{$boundary}/", $body));

    // add boundary for each parameters
    array_walk($body, function (&$part) use ($boundary) {
        $part = "--{$boundary}\r\n{$part}";
    });

    // add final boundary
    $body[] = "--{$boundary}--";
    $body[] = "";

    // set options
    return @curl_setopt_array($ch, array(
        CURLOPT_POST       => true,
        CURLOPT_POSTFIELDS => implode("\r\n", $body),
        CURLOPT_HTTPHEADER => array(
            "Expect: 100-continue",
            "Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type
        ),
    ));
}

This was retrieved from the user contributed notes section of the PHP CURLFile::__construct documentation 这是从PHP CURLFile :: __构造文档的用户提供的注释部分检索的

I used it like so and it worked: 我这样使用它并且它有效:

$fields = array(
    'json' => json_encode( array(
        'method' => "create_video",
        'params' => array(
            'video' => array(
                'name' => $video->submission_by_name.' '.time(),
                'shortDescription' => $video->submission_question_1
            ),
            "token" => $this->config->item('brightcove_write_token'),
            "encode_to" => "MP4",
            "create_multiple_renditions" => "True"
        ))
    )
);

$files = array(
    'file' => FCPATH.'assets/uploads/submitted/'.$video->filename
);

//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $this->config->item('brightcove_write_endpoint'));
$this->curl_custom_postfields($ch, $fields, $files);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = json_decode(curl_exec($ch));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM