繁体   English   中英

发送文件CURL + PHP 7

[英]Send file CURL + PHP 7

我使用代码发送zip文件,这个工作很好:

$file = ''.$_SERVER['DOCUMENT_ROOT'].'/projects/file.zip';
$data = array(
    "file" => new CURLFile($file),
    "data" => '{"title":"Test"}'
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'https://example.com');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
  'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$final = curl_exec($handle);
$response = json_decode($final, true);
curl_close ($handle);

但是,当我发送扩展名为.keystore的文件时,没有任何效果。 我在服务器上没有收到错误。

如何正确发送这样的文件?

你可以试试这个

$file = ''.$_SERVER['DOCUMENT_ROOT'].'/projects/file.zip';
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);

$data = array(
    "file" => $output,
    "data" => '{"title":"Test"}'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
   $result = curl_error($ch);
}
curl_close ($ch);

暂无
暂无

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

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