简体   繁体   English

multipart / form-data csv文件上传POST PHP curl

[英]multipart/form-data csv file upload POST PHP curl

I'm trying to send a file via http POST using PHP and curl. 我正在尝试使用PHP和curl通过http POST发送文件。

I had no problems working with basic fields, however since I tried to upload the file, I still have no luck. 我在使用基本字段时没有任何问题,但是由于我尝试上传文件,因此仍然没有运气。

From the terminal I can easily get it to work with the following command: 从终端,我可以轻松地使用以下命令来使其工作:

curl --header 'Content-Type: multipart/form-data' --header 'auth-token: #.######.##.##########################' -F 'file=@test_csv.csv' -F 'command=import' -F 'parameters={"contact-list_code": "1", "separator": ",", "header": false, "file_columns": [{"Position":"0", "Field":"1", "Rule":"3"}, {"Position":"1", "Field":"14", "Rule":"3"} ] }' http://api.com/method

I tried using the following in php: 我尝试在php中使用以下内容:

$file = '@'.realpath(dirname( __FILE__ ) . '/exports/test_csv.csv');
$data = http_build_query(array(
    "command" => "import",
    "file" => $file,
    "parameters" =>  array(
         "contact-list_code" =>  "1",
         "separator" =>  ",",
         "header" =>  false,
         "file_columns" =>  array(
            array(
                "Position" => "0", 
                "Field" => "1", 
                "Rule" => "3"
            ),
            array(
                "Position" => "1", 
                "Field" => "14", 
                "Rule" => "3"
            )
        )
    )
));
$authToken = '#.######.##.##########################';

$serviceHandler = curl_init();
curl_setopt($serviceHandler, CURLOPT_URL, 'http://api.com/method');
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Auth-Token: ".$authToken;

curl_setopt($serviceHandler, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($serviceHandler, CURLOPT_POST, TRUE);
curl_setopt($serviceHandler, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($serviceHandler, CURLOPT_POSTFIELDS, $data);
curl_setopt($serviceHandler, CURLOPT_HTTPHEADER, $headers);

print_r(json_decode(curl_exec($serviceHandler)));

Here's the error I received: Error handling data from http request 这是我收到的错误: Error handling data from http request

I also tried using CURLFile class for the file instead of the @ for the file, however it gave me the same error. 我也尝试使用CURLFile类而不是@作为文件,但是它给了我同样的错误。

I think it's worth mentioning that I'm running this code inside a Wordpress plugin. 我认为值得一提的是,我正在Wordpress插件中运行此代码。

I wonder what's going wrong with my php code since the curl shell command is fine. 我不知道我的php代码出了什么问题,因为curl shell命令很好。

Thank you. 谢谢。

Dont use http_build_query when constructing $data variable 构造$data变量时不要使用http_build_query

From the documentation: 从文档中:

CURLOPT_POSTFIELDS CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation. 要在HTTP“ POST”操作中发布的完整数据。 To post a file, prepend a filename with @ and use the full path. 要发布文件,请在文件名前添加@并使用完整路径。 The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. 可以通过在文件名后跟随类型'; type = mimetype'的类型来明确指定文件类型。 This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. 此参数可以作为urlencoded字符串(如“ para1 = val1&para2 = val2&...”)传递,也可以作为字段名称为键且字段数据为值的数组传递。 If value is an array, the Content-Type header will be set to multipart/form-data. 如果value是一个数组,则Content-Type标头将设置为multipart / form-data。 As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. 从PHP 5.2.0开始,如果使用@前缀将文件传递给此选项,则value必须是一个数组。 As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. 从PHP 5.5.0开始,不赞成使用@前缀,并且可以使用CURLFile发送文件。 The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE. 通过将CURLOPT_SAFE_UPLOAD选项设置为TRUE,可以禁用@前缀以安全地传递以@开头的值。

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

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