博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CURL POST上传文件
阅读量:7076 次
发布时间:2019-06-28

本文共 2649 字,大约阅读时间需要 8 分钟。

  hot3.png

0) { switch ($_FILES ['Filedata'] ['error']) { case 1 : $error_log = 'The file is bigger than this PHP installation allows'; break; case 2 : $error_log = 'The file is bigger than this form allows'; break; case 3 : $error_log = 'Only part of the file was uploaded'; break; case 4 : $error_log = 'No file was uploaded'; break; default : break; } die ( 'upload error:' . $error_log );} else { $img_data = $_FILES['Filedata']['tmp_name']; $size = getimagesize($img_data); $file_type = $size['mime']; if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) { $error_log = 'only allow jpg,png,gif'; die ( 'upload error:' . $error_log ); } switch($file_type) { case 'image/jpg' : case 'image/jpeg' : case 'image/pjpeg' : $extension = 'jpg'; break; case 'image/png' : $extension = 'png'; break; case 'image/gif' : $extension = 'gif'; break; } }if (!is_file($img_data)) { die ( 'Image upload error!' );}//图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)$save_path = dirname( __FILE__ );$uinqid = uniqid();$filename = $save_path . '/' . $uinqid . '.' . $extension;$result = move_uploaded_file( $img_data, $filename );if ( ! $result || ! is_file( $filename ) ) { die ( 'Image upload error!' );}echo 'Image data save successed,file:' . $filename;$ch = curl_init();$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96, 'file' => new \CURLFile(realpath("$filename")));curl_setopt($ch, CURLOPT_URL, 'http://xxxxxxxxxxxxx');curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$response = curl_exec($ch);$err = curl_error($ch);curl_close($ch);if ($err) { echo "cURL Error #:" . $err;} else { echo $response;}

以上为原生实现

 

ws-http实现:

https://github.com/toohamster/ws-http

'application/json');$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96);$files = array('file' => './logo.jpg');$body = Ws\Http\Request\Body::multipart($data, $files);$response = $httpRequest->post('http://xxxxxxxxx', $headers, $body);

 

unirest实现

https://github.com/Mashape/unirest-php

$headers = array('Accept' => 'application/json');$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96);$files = array('file' => './logo.jpg');$body = Unirest\Request\Body::multipart($data, $files);$response = Unirest\Request::post('http://xxxxxxxxxx', $headers, $body);

 

 

转载于:https://my.oschina.net/yjft/blog/1504493

你可能感兴趣的文章
<备份>10月11日系统安装与除错,sudo讲解
查看>>
网站优化简单探讨
查看>>
samba部署和优化
查看>>
libsuperuser简介
查看>>
Python自动化开发学习-爬虫2
查看>>
Scribe配置文件解析
查看>>
HISTFILESIZE与HISTSIZE的区别
查看>>
Parallels Desktop 8 虚拟机安装 windows 教程
查看>>
51cto博客第一篇
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
使用DataWorks来调度AnalyticDB任务
查看>>
CSV文件读取,解决汉字乱码
查看>>
Squid代理服务基础篇
查看>>
我的友情链接
查看>>
rhel 6.5 x86_64 配置centos yum源
查看>>
redis 的setnx命令
查看>>
在VMware Workstation上安装Kali Linux
查看>>
联想IPMI固件SMASH-CLP 管理
查看>>
glance镜像元数据
查看>>