php5.4 curl,PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析

本文实例分析了PHP5.0~5.6 各版本兼容性cURL文件上传功能。分享给大家供大家参考,具体如下:

最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件。踩坑若干,够一篇文章了。

重要警告

没事不要读PHP的官方中文文档!版本跟不上坑死你!

不同版本PHP之间cURL的区别

PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求。

传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传。这与命令行直接调用cURL程序的语法是一致的:

curl_setopt(ch, CURLOPT_POSTFIELDS, array(

'file' => '@'.realpath('image.png'),

));

equals

$ curl -F "file=@/absolute/path/to/image.png"

但PHP从5.5开始引入了新的CURLFile类用来指向文件。CURLFile类也可以详细定义MIME类型、文件名等可能出现在multipart/form-data数据中的附加信息。PHP推荐使用CURLFile替代旧的@语法:

curl_setopt(ch, CURLOPT_POSTFIELDS, [

'file' => new CURLFile(realpath('image.png')),

]);

PHP 5.5另外引入了CURL_SAFE_UPLOAD选项,可以强制PHP的cURL模块拒绝旧的@语法,仅接受CURLFile式的文件。5.5的默认值为false,5.6的默认值为true。

但是坑的一点在于:@语法在5.5就已经被打了deprecated,在5.6中就直接被删除了(会产生 ErorException: The usage of the@filename API for file uploading is deprecated. Please use the CURLFile class instead)。

对于PHP 5.6+而言,手动设置CURL_SAFE_UPLOAD为false是毫无意义的。根本不是字面意义理解的“设置成false,就能开启旧的unsafe的方式”——旧的方式已经作为废弃语法彻底不存在了。PHP 5.6+ == CURLFile only,不要有任何的幻想。

我的部署环境是5.4(仅@语法),但开发环境是5.6(仅CURLFile)。都没有压在5.5这个两者都支持过渡版本上,结果就是必须写出带有环境判断的两套代码。

现在问题来了……

环境判断:小心魔法数字!

我见过这种环境判断的代码:

if (version_compare(phpversion(), '5.4.0') >= 0)

我对这种代码的评价只有一个字:屎。

这个判断掉入了典型的魔法数字陷阱。版本号莫名其妙的出现在代码之中,不查半天PHP手册和更新历史,很难明白作者被卡在了哪个功能的变更上。

代码应该回归本源。我们的实际需求其实是:有CURLFile就优先采用,没有再退化到传统@语法。那么代码就来了:

if (class_exists('\CURLFile')) {

$field = array('fieldname' => new \CURLFile(realpath($filepath)));

} else {

$field = array('fieldname' => '@' . realpath($filepath));

}

建议明确指定的退化选项

从可靠的角度,推荐指定CURL_SAFE_UPLOAD的值,明确告知php是容忍还是禁止旧的@语法。注意在低版本PHP中CURLOPT_SAFE_UPLOAD常量本身可能不存在,需要判断:

if (class_exists('\CURLFile')) {

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);

} else {

if (defined('CURLOPT_SAFE_UPLOAD')) {

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

}

}

cURL选项设置的顺序

不管是curl_setopt()单发还是curl_setopt_array()批量,cURL的选项总是设置一个生效一个,而设置好的选项立刻就会影响cURL在设置后续选项时的行为。

例如CURLOPT_SAFE_UPLOAD就和CURLOPT_POSTFIELDS的行为有关。如果先设置CURLOPT_POSTFIELDS再设置CURLOPT_SAFE_UPLOAD,那么后者的约束作用就不会生效。因为设置前者时cURL就已经把数据实际的识读处理完毕了!

cURL有那么几个选项存在这种坑,务必小心。还好这种存在“依赖关系”的选项不多,机制也不复杂,简单处理即可。我的方法是先批量设置所有的选项,然后直到curl_exec()的前一刻才用curl_setopt()单发设置CURLOPT_POSTFIELDS。

实际上在curl_setopt_array()用的数组中,保证CURLOPT_POSTFIELDS的位置在后边也是可靠的。PHP的关联数组是有顺序保障的,我们也可以假设curl_setopt_array()内部的执行顺序一定是从头到尾按顺序(好吧我知道assume不是件好事,不过有些实在过分浅显的事实,就容我下个最低限度的断言吧),所以尽可放心。

我的做法只是在代码表现上加个多余的保险,突出强调顺序的重要性防以后手贱。

命名空间

PHP 5.2或以下的版本没有命名空间。代码中用到了空间分隔符\就会引发解析器错误。要照顾PHP 5.2其实容易想,放弃命名空间即可。

要注意的反倒是有命名空间的PHP 5.3+。无论是调用CURLFile还是用class_exists()判断CURLFile的存在性,都推荐写成\CURLFile明确指定顶层空间,防止代码包裹在命名空间内的时候崩掉。

希望本文所述对大家PHP程序设计有所帮助。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441700.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1518】Square (经典的dfs + 剪枝)

题干&#xff1a; Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 < M < 20, the number …

php发扑克牌,php 扑克牌代码的简单例子

本文分享下&#xff0c;一段可模拟扑克牌玩法的php代码&#xff0c;有需要的朋友参考下。php 扑克牌代码&#xff0c;如下&#xff1a;cut();* www: jbxue.com*/class cards{/**** Declare our deck variable**/private $deck;/**** Constructor.. duh!**/function __construct…

【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)

题干&#xff1a; The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*…

php多线程模拟请求,浅谈php使用curl模拟多线程发送请求

每个PHP文件的执行是单线程的&#xff0c;但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程&#xff0c;这里用curl模拟多线程发送请求。php的curl多线程是通过不断调用curl_multi_exec来获取内容&#xff0c;这里举一个demo来模拟一次curl多线程并发操作。//设…

【HDU - 1455】Sticks (dfs + 剪枝)

题干&#xff1a; George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original…

java找不到符号类socket,编译报错+解决方法:错误: 找不到符号

public class ServerPlainTest { // 包内包外可见public static void main(String[] args) {try {ServerSocket ss new ServerSocket(8189);System.out.println("the server has startuped, waiting for connections.");while (true) { // accept multiple clients …

php hbase thrift,PHP使用Thrift操作Hbase

系统架构图HBase 启动 Thrift服务hbase启动thrift服务// 进入安装的hbase bin目录下// 执行hbase-daemon.sh start thrift2需要注意的是&#xff0c;这里启动的是thrift2服务&#xff0c;如果需要启动thrift服务只需要将thrift2改为thrift就可以了&#xff0c;具体thrift和thri…

【CodeForces - 761D 】Dasha and Very Difficult Problem (构造,思维)

题干&#xff1a; Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci  bi -…

php excel下载打不开了,php下载excel无法打开的解决方法

php下载excel文件,1、在下载的过程中不要 输出任何非文件信息&#xff0c;比如 echo log信息。 否则下载后的文件无法打开&#xff0c;提示格式错误或者文件被破坏。2、 输出的excel格式一定要和后缀名保存一直&#xff0c;否也会提示格式错误或者文件被破坏if (file_exists(CA…

【CodeForces - 761B】Dasha and friends (思维,模拟,构造)

题干&#xff1a; Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation: The track is the circle with length L, in distinct points of which there…

php字符串变量,PHP 字符串变量

PHP 字符串变量字符串变量用于存储并处理文本。PHP 中的字符串变量字符串变量用于包含有字符的值。在创建字符串之后&#xff0c;我们就可以对它进行操作了。您可以直接在函数中使用字符串&#xff0c;或者把它存储在变量中。在下面的实例中&#xff0c;我们创建一个名为 txt 的…

【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)

题干&#xff1a; After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements: There is at least one digit in the string,There is a…

php4和php5的区别,什么是PHP 4和PHP 5之间的区别是什么-php是什么文件

&#xff1f;尽管PHP 5是故意设计成兼容尽可能与以前的版本&#xff0c;也有一些显著的变化。 其中的一些变化包括&#xff1a; 一个新的OOP模型基础上&#xff0c;Zend引擎2.0 改进MySQL支持的一个新推广 内置SQLite的原生支持 一个新的错误报告不断&#xff0c; E_STRICT &am…

【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)

题干&#xff1a; Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farme…

php 向公众号发送消息,微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。即客户要求&#xff0c;他在后台编辑好文章后要主动给每个用户都发送消息&#xff0c;并可以让用户点击直接进入文章页面。于是乎&#xff0c;当时脑子一热&#xff0c;想着没什么大的问题&#xff0c;so easy。模板消息不就得了。…

【CodeForces - 764A】Taymyr is calling you (找规律,水题)

题干&#xff1a; Comrade Dujikov is busy choosing artists for Timofeys birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, …

ecshop php升级,升级-安装与升级- ECShop帮助

ECShop V2.6.2版本有GBK和UFT-8两种编码格式的程序&#xff0c;而低于ECShop V2.6.0 的版本只有UTF-8 编码的(这些版本包括 ECShop 2.5.1、ECShop 2.5.0、ECShop 2.1.5、ECShop 2.1.2b等)&#xff0c; 这些版本升级到ECShop V2.6.2均可以使用本程序升级。相同版本的升级只需要覆…

【CodeForces - 764B 】Timofey and cubes (模拟)

题干&#xff1a; Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Tim…

php页面转发,php如何实现页面路由转发

php实现页面路由转发的方法&#xff1a;首先配置nginx服务器&#xff0c;在【.htaccess】中写上nginx的语法&#xff1b;然后打开根目录的【index.php】&#xff0c;编写文件路由即可。php实现页面路由转发的方法&#xff1a;1、配置nginx服务器nginx服务器不会自动读取.htacce…

【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

题干&#xff1a; One of Timofeys birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, bu…