最近在做一个接口,接口中需要上传文件,一看肯定是需要使用curl无疑,让我再次记录: 
一、最简单的例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | $request = curl_init('http://example.com/');
 
 
 curl\_setopt($request, CURLOPT\_POST, true);
 curl_setopt(
 $request,
 CURLOPT_POSTFIELDS,
 array(
 'file' => '@' . realpath('example.txt')
 ));
 
 
 curl\_setopt($request, CURLOPT\_RETURNTRANSFER, true);
 echo curl_exec($request);
 
 
 curl_close($request);
 
 | 
二、设置文件名:
| 12
 3
 4
 5
 6
 
 | curl_setopt($request,
 CURLOPT_POSTFIELDS,
 array(
 'file' => '@' . realpath('example.txt') . ';filename=name.txt'
 ));
 
 | 
  三、如果是在 表单中上传的:
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | curl_setopt($request,
 CURLOPT_POSTFIELDS,
 array(
 'file' =>
 '@'            . $\_FILES\['file'\]\['tmp\_name'\]
 . ';filename=' . $_FILES\['file'\]\['name'\]
 . ';type='     . $_FILES\['file'\]\['type'\]
 ));
 
 | 
在表单中上传则会 先将文件上传到服务器,然后再上传到curl的服务器