如何发送文件上传使用Php卷曲没有输入类型文件控制?

问题描述:

我要上传从一个服务器到另一个文件,而无需使用文件控制和使用curl请求..如何发送文件上传使用Php卷曲没有输入类型文件控制?

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'api-token: 6a53bcbe222c490196e4b9f87ba9148c' --header 'api-secret: 6a53bcbe222c490196e4b9f87ba9148c' -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" 'https://server_api_url.com 

' 上述卷曲正常工作...

但我不知道如何发布在PHP文件... 下面是示例代码如何我使用PHP卷曲

<?php 

     $doc_file_path_string = ' -F document[][email protected]_name'; 
     array_push($doc_data_array, $doc_file_path_string);  


     $url = 'https://server_url.com'; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, implode(' ',$doc_data_array)); 
     $headers = array(); 
     $headers[] = "Content-Type: multipart/form-data"; 
     $headers[] = "Accept: application/json"; 
     $headers[] = "Api-Token: api_key"; 
     $headers[] = "Api-Secret: api_key "; 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     echo $result = curl_exec($ch); 
    ?> 

这里是我是如何做到的发送POST数据

$url = <some url>; 
$ch = curl_init(); 
$headers = ["Content-Type:multipart/form-data"]; 

// Define curl options 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 

// Here we make base64 encode for file content and push it into curl   
$base64 = base64_encode(file_get_contents(<path to file>)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $base64]); 

// Make request 
$response = curl_exec($ch); 
curl_close($ch); 
+0

嗨,我的回答有帮助吗? – Kison