phpcurl上传文件

161 阅读1分钟
$file = realpath('gif/1.gif'); //要上传的文件
$fields['f'] = '@'.$file;
 
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL,"http://localhost/ajax_server.php?id=1");  
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
 
curl_exec ($ch);  
curl_close ($ch);  

php5.6后@不能用了需要CURLFile这个函数,下面就是

<?php

$url = "https://api.yunhetong.com/api/template/upload";
 
 $file = realpath("./技术咨询服务合同.docx"); //要上传的文件
 
$post_data = [
"templateName" => "技术咨询服务合同测试",
"multipartFile" =>new CURLFile($file) 
];  
 
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,["token:"."eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJWN1ZqRUNOOElEd2dJVXNNQWxCUTZka2lORk9oRkhkdU44a3J2UEpOUGNZPSIsImV4cCI6MTYyMzIzMDMwM30.yoCVn3oxoRsr0YyjRVBlyZosKRIdpaChSJwNLegpN1ACx4bgvcR3YjJ08zUzeu_a7xsfXLQ5Zh7icTENxka-ng"]); 
curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data); 
$output = curl_exec($ch);  
curl_close($ch); 
echo $output; 
die;
?>