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

    REFRENCES:http://blog.csdn.net/hitlion2008/article/details/7983449 AsyncTask的介绍及基本使用方法 关于AsyncTask的介 ...

  2. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...

  3. CentOS Linux 下安装Samba

    一.Samba简介: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.Linux中搭建环境有几个需要使用的软件包: (1)samba-client:这个 ...

  4. dom元素改变监听

    function domChange(domId, callback) { // select the target node var target = document.getElementById ...

  5. otunnel : 一个和lcx差不多的端口转发的工具

    项目地址 ooclab/otunnel 下载地址(内涵各大平台) http://dl.ooclab.com/otunnel/ otunnel 用法 前提: 1. 假设 server 的地址为 exam ...

  6. js使用正则表达式从url中获取参数值

    //从url中获取参数值 function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^ ...

  7. 使用GitHub和Eclipse进行javaEE开发步骤

    下载Git客户端:链接:http://pan.baidu.com/s/1jIueUEy 密码:7gef; 下载Eclipse javaee客户端:http://www.eclipse.org/down ...

  8. 简单工厂模式(simple factory pattern)

    与一个对象相关的职责通常有3类: 1.对象本身所具有的职责(对象自身所具有的数据和行为) 2.创建对象的职责 3.使用对象的职责: 简单工厂模式的缺点: 1.简单工厂集中了所有产品的创建逻辑,职责过重 ...

  9. LR通用的性能分析流程

    Step1:从分析Summary的事务执行情况入手Summary主要是判定事务的响应时间与执行情况是否合理.如果发现问题,则需要做进一步分析.通常情况下,如果事务执行情况失败或响应时间过长等,都需要做 ...

  10. Oracle:create pfile from spfile:rac下要小心该操作啊!

    默认在原位置创建一个pfile的ora初始化参数文件!! 这在rac下会带来问题,因为rac下,当使用asm存储时,instance的启动参数文件就是pfile(其内容是指向一个spfile).如果不 ...