PHP通过curl模拟POST上传文件,5.5之前和之后的区别
首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题。这里就要说下curl_setopt($ch, CURLOPT_POSTFIELDS, $array) 这个方法上传,在5.5之前是可以用的,5.5的时候已经设置为deprecated,会有下面的提示,5.6的时候已经被删除。所以5.6版本的可能不能直接使用网上的一些代码。
curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead
因此这部分要根据版本判断下,修改为下面
/**
* CURL 上传文件
* @param $url 处理上传文件的url
* @param array $post_data post 传递的参数
* @param array $file_fields 上传文件的参数,支持多个文件上传
* @param int $timeout 请求超时时间
* @return array|bool
*/
function curl_upload($url, $post_data=array(), $file_fields=array(), $timeout=600) {
$result = array('errno' => 0, 'errmsg' => '', 'result' => ''); $ch = curl_init();
//set various curl options first // set url to post to
curl_setopt($ch, CURLOPT_URL, $url); // return into a variable rather than displaying it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //set curl function timeout to $timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//curl_setopt($ch, CURLOPT_VERBOSE, true); //set method to post
curl_setopt($ch, CURLOPT_POST, true); // disable Expect header
// hack to make it working
$headers = array("Expect: ");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //generate post data
$post_array = array();
if (!is_array($post_data)) {
$result['errno'] = 5;
$result['errmsg'] = 'Params error.';
return $result;
} foreach ($post_data as $key => $value) {
$post_array[$key] = $value;
} // set multipart form data - file array field-value pairs
if(version_compare(PHP_VERSION, '5.5.0') >= 0) {
if (!empty($file_fields)) {
foreach ($file_fields as $key => $value) {
if (strpos(PHP_OS, "WIN") !== false) {
$value = str_replace("/", "\\", $value); // win hack
}
$file_fields[$key] = new CURLFile($value);
}
}
} else {
if (!empty($file_fields)) {
foreach ($file_fields as $key => $value) {
if (strpos(PHP_OS, "WIN") !== false) {
$value = str_replace("/", "\\", $value); // win hack
}
$file_fields[$key] = "@" . $value;
}
}
} // set post data
$result_post = array_merge($post_array, $file_fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $result_post);
// print_r($result_post); //and finally send curl request
$output = curl_exec($ch);
$result['result'] = $output; if (curl_errno($ch)) {
echo "Error Occured in Curl\n";
echo "Error number: " . curl_errno($ch) . "\n";
echo "Error message: " . curl_error($ch) . "\n";
return false;
} else {
return $result;
}
curl_close($ch);
}
PHP通过curl模拟POST上传文件,5.5之前和之后的区别的更多相关文章
- 通过PHP CURL模拟请求上传文件|图片。
现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...
- PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)
<?php /** * Email net.webjoy@gmail.com * author jackluo * 2014.11.21 * */ //* function curl_post( ...
- Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)
先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...
- ApiPost接口调试工具模拟Post上传文件(中文版Postman)
ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...
- c# 模拟POST上传文件到服务器
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- PHP中curl模拟post上传及接收文件
public function Action_Upload(){ $this->path_config(); exit(); $furl="@d:\develop\JMFramewor ...
- Linux 基础命令-CURL 表单上传文件
CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...
- 通过WebClient模拟post上传文件到服务器
写在前面 最近一直在研究sharepoint的文档库,在上传文件到文档库的过程中,需要模拟post请求,也查找了几种模拟方式,webclient算是比较简单的方式. 一个例子 这里写一个简单接受pos ...
- 用iFrame模拟Ajax上传文件
前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...
随机推荐
- Linux 获取登录者IP
在linux中有时须要获得登录者的IP,这里有两种方法.先使用who am i 获取登录IP,然后截取字符串: 1.awk截取,sed替换 who am i | awk '{print $5}' | ...
- 【Java NIO的深入研究6】JAVA NIO之Scatter/Gather
Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作. 分散(s ...
- mysql压缩包的安装、配置、配成windows服务、远程连接及常规问题
1.下载windows安装包 下载地址:mysql-5.7.18 2.配置my.ini [client] port = 3306 # 设置mysql客户端连接服务端时默认使用的端口 [mysql] d ...
- c++ template<typename T>
template <typename T> 网上查了半天不知所云,网上说的太多,俺只是要知道所需要的就可以了. 写了个程序试了一下,其实就是这个东西可以根据你所需要的类型就行匹配.其实就是 ...
- XLua系统学习
官方网站:https://github.com/Tencent/xLua 学习手册:http://manual.luaer.cn/ 技术博客: http://blog.csdn.net/column/ ...
- 延迟是AR/VR体验的基础
原文: http://blogs.valvesoftware.com/abrash/latency-the-sine-qua-non-of-ar-and-vr/ 译者注: 原文发表于2012年, 尽管 ...
- VS-Qt-OSG-CMake基本项目框架
在VS-Qt-CMake基础上,打开mainwindow.ui,添加一个QWidget,然后在widget上右键-提升,选择SceneViewWidget CMakeLists.txt中添加了OSG相 ...
- 浅谈Socket长连+多线程
缘由 不知各位同仁有没有发现,用简单,无外乎就是都是一个流程 1)监听链接 2)校验链接是否是正常链接 3)保存链接至全局静态字典 4)开启新线程监听监听到的线程报文 5)执行对应命令或者发送对应命令 ...
- View的setTag()与getTag()方法使用
通常我们是用findViewById()方法来取得我们要使用的View控件,不过除了这一种方法之处 ,我们还可以用View中的setTag(Onbect)给View添加一个格外的数据,再用getTag ...
- 《C++ Primer Plus》12.6 复习各种(类和动态内存分配的)技术 笔记
12.6.1 重载<<运算符要重新定义<<运算符,以便将它和cout一起用来显示对象的内容,请定义下面的友元运算符函数:ostream & operator<&l ...