Php Curl发送文件和数组数据

Wim*_*oen 6 php upload curl file

我想用Curl发送复杂的Post数据.

我尝试发送的数据:

Array
(
    [test] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [file] => CURLFile Object
        (
            [name] => H:\wwwroot\curl/upload.txt
            [mime] => 
            [postname] => 
        )

)
Run Code Online (Sandbox Code Playgroud)

我需要使用post-side中的变量作为$ _POST ["test"]和$ _FILES ["file"]但我无法实现.对于(有时是多维的)数组数据,我需要http_build_query但是会破坏文件.如果我不使用http_build_query,我的数组会给出"数组到字符串转换"错误.

我怎样才能使这个工作?码:

的index.php

$curl = curl_init();

$postValues = Array("test" => Array(1,2,3));
$postValues["file"] = new CurlFile(dirname(__FILE__). "/upload.txt");

curl_setopt($curl, CURLOPT_URL, "localhost/curl/post.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postValues);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$curlResult = curl_exec($curl);
$curlStatus = curl_getinfo($curl);

echo $curlResult;
Run Code Online (Sandbox Code Playgroud)

post.php中

print_r($_REQUEST);
print_r($_FILES);
Run Code Online (Sandbox Code Playgroud)

Wim*_*oen 2

经过长时间的研究和测试,我得到了(不是很好但有效)解决方案:

function createHttpHeader($postValues, $overrideKey = null) {
    global $delimiter;
    // invalid characters for "name" and "filename"
    $disallow = array("\0", "\"", "\r", "\n");

    $data = Array();

    if (!is_array($postValues)) {
        $postValues = Array($postValues);
    }

    foreach($postValues as $key => $value) {
        $useKey = $overrideKey === null ? $key : $overrideKey. "[$key]";
        $useKey = str_replace($disallow, "_", $useKey);

        if (is_array($value)) {
            $data = array_merge($data, addPostData($value, $useKey));
        } else {
            $data[] = "--". $delimiter. "\r\n";
            $data[] = "Content-Disposition: form-data; name=\"". $useKey. "\"";

            if (is_a($value, "\CurlFile")) {
                $data[] = "; filename=\"". basename($value->name). "\"\r\n";
                $data[] = "Content-Type: application/octet-stream\r\n\r\n";
                $data[] = file_get_contents($value->name). "\r\n";
            } else {
                $data[] = "\r\n\r\n". $value. "\r\n";
            }
        }
    }

    return $data;
}
Run Code Online (Sandbox Code Playgroud)

测试:

$postValues = Array(
    "blaat" => 1,
    "test" => Array(1,2,3),
    "grid" => Array(0 => array(1,2), 1 => array(4,5)),
    "gridComplex" => Array("rows" => array(1,2), "columns" => array(0 => array(1,2,3,4), 1 => array(4,5,4,5)))
 );

$postValues["file[0]"] = new CurlFile($file, "text/plain");
$postValues["file[1]"] = new CurlFile($file, "text/plain");
// print_r(new CurlFile($file));exit;

$delimiter = "-------------" . uniqid();
$data = createHttpHeader($postValues);

$data[] = "--" . $delimiter . "--\r\n";
$data = implode("", $data);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "localhost/curl/post.php");
curl_setopt($curl, CURLOPT_HTTPHEADER , array('Content-Type: multipart/form-data; boundary=' . $delimiter, 'Content-Length: ' . strlen($data)));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$curlResult = curl_exec($curl);
echo $curlResult;
Run Code Online (Sandbox Code Playgroud)

编辑:添加 addPostData 函数:

    function addPostData($postValues, $delimiter, $overrideKey = null) {
        // invalid characters for "name" and "filename"
        $disallow = array("\0", "\"", "\r", "\n");

        $data = Array();

        if (!is_array($postValues)) {
            $postValues = Array($postValues);
        }

        foreach($postValues as $key => $value) {
            $useKey = $overrideKey === null ? $key : $overrideKey. "[$key]";
            $useKey = str_replace($disallow, "_", $useKey);

            if (is_array($value)) {
                $data = array_merge($data, $this->addPostData($value, $delimiter, $useKey));
            } else {
                $data[] = "--". $delimiter. "\r\n";
                $data[] = "Content-Disposition: form-data; name=\"". $useKey. "\"";

                if (is_a($value, "\CurlFile")) {
                    $data[] = "; filename=\"". basename($value->postname). "\"\r\n";
                    $data[] = "Content-Type: ". $value->mime. "\r\n\r\n";
                    $data[] = file_get_contents($value->name). "\r\n";
                } else {
                    $data[] = "\r\n\r\n". $value. "\r\n";
                }
            }
        }

        return $data;
    }
Run Code Online (Sandbox Code Playgroud)