不同版本PHP之间cURL的区别(-经验之谈),不同版本curl
不同版本PHP之间cURL的区别(-经验之谈),不同版本curl
之前在做一个采集的工具,实现采集回来的文章,图片保存起来.文章内容是保存在数据库,图片是先需要上传到图片服务器,再返回图片地址,替换掉文章的图片地址.
问题来了:都能成功采集都东西,但是,本地测试是正常的,图片也可以上传成功,但是生产环境就是一直没有图片.然后自己就一步一步调试,,发现数据都有,但为什么偏偏生产上没有成功上传图片呢.
后来折腾了几天,经过一步步的看代码,调试,百度,终于找到答案了.真是一个大坑.
上传到图片服务器是用curl post过去的,
PHP的cURL支持通过给CURL_POSTFIELDS
传递关联数组(而不是字符串)来生成multipart/form-data
的POST请求。
传统上,PHP的cURL支持通过在数组数据中,使用“@
+文件全路径”的语法附加文件,供cURL读取上传。这与命令行直接调用cURL程序的语法是一致的:
<code>curl_setopt(ch, CURLOPT_POSTFIELDS, <span class="hljs-keyword">array( <span class="hljs-string">'file' => <span class="hljs-string">'@'.realpath(<span class="hljs-string">'image.png'), )); equals $ curl -F <span class="hljs-string">"file=@/absolute/path/to/image.png" <url> </span></span></span></span></span></code>
但PHP从5.5开始引入了新的CURLFile类用来指向文件。CURLFile类也可以详细定义MIME类型、文件名等可能出现在multipart/form-data数据中的附加信息。PHP推荐使用CURLFile替代旧的@
语法:
<code>curl_setopt(ch, CURLOPT_POSTFIELDS, [ <span class="hljs-string">'file' => <span class="hljs-keyword">new CURLFile(realpath(<span class="hljs-string">'image.png')), ]); </span></span></span></code>
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这个两者都支持过渡版本上,结果就是必须写出带有环境判断的两套代码。
现在问题来了……
环境判断:小心魔法数字!
我见过这种环境判断的代码:
<code><span class="hljs-keyword">if (version_compare(phpversion(), <span class="hljs-string">'5.4.0') >= <span class="hljs-number">0) </span></span></span></code>
我对这种代码的评价只有一个字:屎。
这个判断掉入了典型的魔法数字陷阱。版本号莫名其妙的出现在代码之中,不查半天PHP手册和更新历史,很难明白作者被卡在了哪个功能的变更上。
代码应该回归本源。我们的实际需求其实是:有CURLFile就优先采用,没有再退化到传统@
语法。那么代码就来了:
<code><span class="hljs-keyword">if (class_exists(<span class="hljs-string">'\CURLFile')) { <span class="hljs-variable">$field = <span class="hljs-keyword">array(<span class="hljs-string">'fieldname' => <span class="hljs-keyword">new \CURLFile(realpath(<span class="hljs-variable">$filepath))); } <span class="hljs-keyword">else { <span class="hljs-variable">$field = <span class="hljs-keyword">array(<span class="hljs-string">'fieldname' => <span class="hljs-string">'@' . realpath(<span class="hljs-variable">$filepath)); } </span></span></span></span></span></span></span></span></span></span></span></span></span></code>
建议明确指定的退化选项
从可靠的角度,推荐指定CURL_SAFE_UPLOAD
的值,明确告知php是容忍还是禁止旧的@
语法。注意在低版本PHP中CURLOPT_SAFE_UPLOAD
常量本身可能不存在,需要判断:
<code><span class="hljs-keyword">if (class_exists(<span class="hljs-string">'\CURLFile')) { curl_<span class="hljs-built_in">setopt(<span class="hljs-variable">$ch, CURLOPT_SAFE_UPLOAD, <span class="hljs-literal">true); } <span class="hljs-keyword">else { <span class="hljs-keyword">if (defined(<span class="hljs-string">'CURLOPT_SAFE_UPLOAD')) { curl_<span class="hljs-built_in">setopt(<span class="hljs-variable">$ch, CURLOPT_SAFE_UPLOAD, <span class="hljs-literal">false); } } </span></span></span></span></span></span></span></span></span></span></span></code>
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()
内部的执行顺序一定是从头到尾按顺序[注A]
,所以尽可放心。
我的做法只是在代码表现上加个多余的保险,突出强调顺序的重要性防以后手贱。
命名空间
PHP 5.2或以下的版本没有命名空间。代码中用到了空间分隔符\
就会引发解析器错误。要照顾PHP 5.2其实容易想,放弃命名空间即可。
要注意的反倒是有命名空间的PHP 5.3+。无论是调用CURLFile还是用class_exists()
判断CURLFile的存在性,都推荐写成\CURLFile
明确指定顶层空间,防止代码包裹在命名空间内的时候崩掉。
好了,这坑挖得好深,跳出来就分享下.
(以上解决方法是转载网站的,感谢让我找到了你这篇东西!)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
