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

    Java中处理图片时,MyEclipse需要导入以下包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.j ...

  2. 20160216.CCPP体系具体解释(0026天)

    程序片段(01):01.MemCpy.c 内容概要:内存拷贝 #include <stdio.h> #include <stdlib.h> #include <memor ...

  3. par函数的new 参数-进行图片的叠加

    默认调用高级绘图函数的时候,会另外在绘制一副图,为了将两幅图绘制在同一张纸上,可以设置new = TRUE 代码示例: plot(1:5, c(2,3,2,3,2), col = "red& ...

  4. CentOS学习之常用命令ls

    命令格式与目录处理命令ls 命令格式:  命令[-选项][参数] 例如:  ls -la /etc 说明: 1)个别命令使用不遵循此格式 2)当多个选项时,可以写在一起 3)简化选项与完整选项 -a  ...

  5. Windows下配置Apache+PHP跑Wordpress拾遗

    首先,我很少这么做,因为一旦有跑WAMP的需求,我就直接下一个wamp的安装包就可以了,市面上数不胜数,我一直用的是EasyPHP,不是说它有多好,而是很多年前第一次用后没什么问题,就一直用下来了.这 ...

  6. Git 基础 - 查看提交历史

    查看提交历史 在提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,可以使用 git log 命令查看. 接下来的例子会用我专门用于演示的 simplegit 项目,运行下面的命令获取该项目源 ...

  7. easyui------添加中文文件

    添加中文文件: 官网地址:http://www.jeasyui.net/download/去jquery-easyui官网下载的文件里面找到easyui-lang-zh_CN.js文件,添加入代码里面 ...

  8. ios 显示代码块(show the code snippet library)

    在项目的实际开发中,我们会重复的书写很多的代码,我经常是需要用到某一个功能,就从以前的项目中复制粘贴过来,很是麻烦 下面就为大家提供两种不错的方法, 一.宏定义,这个大家应该很熟悉,在这里就不做多的介 ...

  9. python2.0_s12_day21_web聊天室一

    本节内容: 项目实战:开发一个WEB聊天室 功能需求: 用户可以与好友一对一聊天 可以搜索.添加某人为好友 用户可以搜索和添加群 每个群有管理员可以审批用户的加群请求,群管理员可以用多个,群管理员可以 ...

  10. Python 进阶(三)面向对象编程基础

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkMAAAFGCAIAAADmfgziAAAgAElEQVR4nOx993vT1v7/93/5EEt2Eg