4

我将图像资源存储在变量中,我需要使用其 http API 和 PHP 将其发送到服务器。我必须发送内容类型为 multipart/form-data 的请求。所以,我需要像发送带有文件输入和 enctype=multipart/form-data 属性的表单一样发出类似的请求。我试过这个:

<?php
$url = 'here_is_url_for_web_API';
$input = fopen('delfin.jpg','r');       
$header = array('Content-Type: multipart/form-data');
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_USERPWD, "user:password");
curl_setopt($resource, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true );
curl_setopt($resource, CURLOPT_INFILESIZE, 61631);
curl_setopt($resource, CURLOPT_INFILE, $input);
$result = curl_exec($resource);
curl_close($resource);
var_dump($result);
?>

我不知道响应应该是什么样子,但这会返回:http 状态 405 并且错误报告是:请求的资源()不允许指定的 HTTP 方法。

4

2 回答 2

7

在带有 curl 的 POST 内容中使用 multipart/form-data 和边界。

$filenames = array("/tmp/1.jpg", "/tmp/2.png");

$files = array();
foreach ($filenames as $f){
    $files[$f] = file_get_contents($f);
}

// more fields for POST request
$fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
    
$url = "http://example.com/upload";

$curl = curl_init();

$url_data = http_build_query($data);

$boundary = uniqid();
$delimiter = '-------------' . $boundary;

$post_data = build_data_files($boundary, $fields, $files);

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    //CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $post_data,
    CURLOPT_HTTPHEADER => array(
        //"Authorization: Bearer $TOKEN",
        "Content-Type: multipart/form-data; boundary=" . $delimiter,
        "Content-Length: " . strlen($post_data)
    )
));

//
$response = curl_exec($curl);

$info = curl_getinfo($curl);
//echo "code: ${info['http_code']}";

//print_r($info['request_header']);

var_dump($response);
$err = curl_error($curl);

echo "error";
var_dump($err);
curl_close($curl);
    
function build_data_files($boundary, $fields, $files){
    $data = '';
    $eol = "\r\n";

    $delimiter = '-------------' . $boundary;

    foreach ($fields as $name => $content) {
        $data .= "--" . $delimiter . $eol
              . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
              . $content . $eol;
    }

    foreach ($files as $name => $content) {
        $data .= "--" . $delimiter . $eol
              . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
              //. 'Content-Type: image/png'.$eol
              . 'Content-Transfer-Encoding: binary'.$eol;
    
        $data .= $eol;
        $data .= $content . $eol;
    }
    $data .= "--" . $delimiter . "--".$eol;

    return $data;
}

在此处查看完整示例:https ://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f

于 2017-08-23T19:04:23.653 回答
1

如果您使用 CURL,您只需:

1、将标题'Content-Type'设置为'multipart/form-data;'

2、将curl的option 'RETURNTRANSFER'设置为true(使用curl的option方法)

3、设置curl的选项'POST'为true(使用curl的选项方法)

4、获取你的文件的来源(你从 PHP 中的 fopen 得到的):

$tempFile = tempnam(sys_get_temp_dir(), 'File_');                
file_put_contents($tempFile, $source);
$post = array(
    "uploadedFile" => "@" . $tempFile, //"@".$tempFile.";type=image/jpeg",
);

5、在$post变量中使用带参数的CURL的post方法

于 2014-01-05T18:45:54.707 回答