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

    MJRefresh是流行的下拉刷新控件.前段时间为了修复一个BUG.读了它的源代码.本文总结一下实现的原理 下拉刷新的基本原理 大部分的下拉刷新控件.都是用contentInset实现的.默认情况下. ...

  2. [CS]C#操作word

    近期在做的项目已经改了好几版,近期这一版用到了word,当然不是直接使用word,而是使用第三方的ActiveX控件:dsoframer.ocx.此控件的使用和其它控件的使用流程没有不论什么差别.接下 ...

  3. 【Java面试题】55 说说&和&&的区别。

    &和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false. ...

  4. jQuery操作属性和样式详解

    我们可以使用 javascript 中的getAttribute和setAttribute来操作元素的"元素属性".在 jQuery 中给你提供了attr()包装集函数, 能够同时 ...

  5. go语言的time.Sleep

    首先:time.sleep单位为:1ns (纳秒) 转换单位: 1纳秒 =1000皮秒      1纳秒 =0.001 微秒      1纳秒 =0.000 001毫秒        1纳秒 =0.0 ...

  6. [ML] I'm back for Machine Learning

    Hi, Long time no see. Briefly, I plan to step into this new area, data analysis. In the past few yea ...

  7. C# Smtp方式发送邮件

    //简单邮件传输协议类             System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();        ...

  8. python 数据类型详解(转)

    转自:http://www.cnblogs.com/linjiqin/p/3608541.html 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8.字典9.日期 1.字符串1. ...

  9. Burp Suite使用教程

    http://www.nxadmin.com/tools/689.html http://tech.idv2.com/2006/08/31/burp-suite/ http://www.securit ...

  10. Python-Numpy的tile函数用法

    1.函数的定义与说明 函数格式tile(A,reps) A和reps都是array_like A的类型众多,几乎所有类型都可以:array, list, tuple, dict, matrix以及基本 ...