首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题。这里就要说下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之前和之后的区别的更多相关文章

  1. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

  2. PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)

    <?php /** * Email net.webjoy@gmail.com * author jackluo * 2014.11.21 * */ //* function curl_post( ...

  3. Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)

    先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...

  4. ApiPost接口调试工具模拟Post上传文件(中文版Postman)

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...

  5. c# 模拟POST上传文件到服务器

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. PHP中curl模拟post上传及接收文件

    public function Action_Upload(){ $this->path_config(); exit(); $furl="@d:\develop\JMFramewor ...

  7. Linux 基础命令-CURL 表单上传文件

    CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...

  8. 通过WebClient模拟post上传文件到服务器

    写在前面 最近一直在研究sharepoint的文档库,在上传文件到文档库的过程中,需要模拟post请求,也查找了几种模拟方式,webclient算是比较简单的方式. 一个例子 这里写一个简单接受pos ...

  9. 用iFrame模拟Ajax上传文件

    前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...

随机推荐

  1. mac下zephir第一步,安装+hello zephir!

    第一步 安装autoconf re2c automake libtool等依赖,如果没有安装git的记得先安装git brew install autoconf brew install automa ...

  2. Nginx的启动与停止,重启

    1.先确定nginx所在的文件位置 如: 重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 2.重启Nginx服务 方法一:进入ngin ...

  3. MySQL 日期与时间的处理

    1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysdate(). mysql> select now(),localtime(),cu ...

  4. [转]Git学习笔记与IntelliJ IDEA整合

    Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...

  5. jQuery弹出层效果

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  6. mysql中,如何查看数据库元数据(metadata)的字符集?

    需求描述: mysql中,数据库的元数据也是有字符集的. 操作过程: 1.查看mysql数据库元数据的字符集 mysql> show variables like 'character_set_ ...

  7. input 监听输入事件

    $("#" + inputId).on("input", function () { var checkboxId = $("#" + in ...

  8. 漫谈.Net关键字系列之一Sealed与Final(转)

    转自:http://www.cnblogs.com/isline/archive/2010/08/31/1813396.html Sealed与Final修饰符其实并不是一个语言平台的产物,他们有着各 ...

  9. iOS根据文字字数动态确定Label宽高

    我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...

  10. 当新增页面和编辑页面使用同一jsp时

    <c:if test="${type eq '1'}"><title>新增页面</title></c:if> <c:if te ...