Differences between different versions of PHP cURL (-experience), different versions of curl_PHP tutorial

Source: Internet
Author: User
Differences between different versions of PHP cURL (-experience) and different versions of curl. The difference between different versions of PHP cURL (-experience). different versions of curl are used to collect articles and save the images. the content of this article is the difference between cURL stored in different PHP versions of the database (-experience), different versions of curl

I am working on a collection tool to save the Collected Articles and images. the content of the article is stored in the database, and the image needs to be uploaded to the image server first, and then the image address is returned, replacing the image address of the article.

The problem is that everything can be collected successfully. However, local testing is normal and images can be uploaded successfully, but there is no image in the production environment. then I debug it step by step and find that all the data is available, but why did I fail to upload images in production.

After a few days, I finally found the answer after reading the code, debugging, and Baidu step by step.

The uploaded image server uses curl post,

PHP cURL supportsCURL_POSTFIELDSTransmits the correlated array (rather than the string) to generatemultipart/form-data.

Traditionally, PHP cURL supports using the"@+ File full path "syntax appended file for cURL to read and upload. This is consistent with the syntax for directly calling the cURL program through the command line:

curl_setopt(ch, CURLOPT_POSTFIELDS, array(    'file' => '@'.realpath('image.png'), )); equals$ curl -F "file=@/absolute/path/to/image.png" 
  

However, PHP introduced a new CURLFile class from 5.5 to point to a file. The CURLFile class can also define the MIME type, file name, and other additional information that may appear in multipart/form-data. We recommend that you use CURLFile to replace the old@Syntax:

curl_setopt(ch, CURLOPT_POSTFIELDS, [    'file' => new CURLFile(realpath('image.png')), ]); 

PHP 5.5 has introducedCURL_SAFE_UPLOADOption to force the PHP cURL module to reject the old@Syntax. only files in the CURLFile format are accepted. The default value of 5.5 is false, and the default value of 5.6 is true.

However, the pitfalls are:@The syntax is deprecated in 5.5 and deleted directly in 5.6 (ErorException: the usage of@filenameAPI for file uploading is deprecated. Please use the CURLFile class instead ).

For PHP 5.6 +, manually setCURL_SAFE_UPLOADFalse isMeaningless. It is not literally understood as "set to false, you can enable the old unsafe method"-the old method is no longer used as an obsolete syntax. PHP 5.6 + = CURLFile only, do not have any fantasies.

My deployment environment is 5.4 (only@But the development environment is 5.6 (only CURLFile ). Neither version 5.5 nor supports the transitional version. The result is that two sets of code with environment judgment must be written.

Now the problem is ......

Environment judgment: be careful with magic numbers!

I have seen code for this environment judgment:

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

I have only one word for this code:Shit.

This judgment falls into a typicalMagic number trap. The version number appears inexplicably in the code. without checking the PHP manual and update history for half a day, it is difficult to understand which function The author is stuck in.

The code should return to the source. Our actual requirement is: the use of CURLFile is preferred without degrading to the traditional@Syntax. Then the code comes:

if (class_exists('\CURLFile')) {    $field = array('fieldname' => new \CURLFile(realpath($filepath)));} else {    $field = array('fieldname' => '@' . realpath($filepath));}
We recommend that you specify degradation options explicitly.

From a reliable perspective, it is recommended to specifyCURL_SAFE_UPLOADTo explicitly tell php whether to tolerate or disable the old one.@Syntax. Note that in earlier PHP versionsCURLOPT_SAFE_UPLOADThe constant itself may not exist and must be judged as follows:

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 option setting order

No mattercurl_setopt()Single orcurl_setopt_array()In batch, the cURL option is always set to take effect, and the setting immediately affects the behavior of cURL when setting subsequent options.

For exampleCURLOPT_SAFE_UPLOADJust sumCURLOPT_POSTFIELDS. If you setCURLOPT_POSTFIELDSSet againCURLOPT_SAFE_UPLOAD, Then the binding effect of the latter will not take effect. Because when the former is set, the cURL has completed the actual read and write of the data!

There are several options in cURL, so be careful. Fortunately, there are not many options for "dependency", and the mechanism is not complex. you can simply process them. My method is to first set all the options in batches, and thencurl_exec()The last moment beforecurl_setopt()Single-click settingCURLOPT_POSTFIELDS.

In factcurl_setopt_array()In the array to ensureCURLOPT_POSTFIELDSThe location is also reliable.PHP's associated arrays are ordered., We can also assume thatcurl_setopt_array()The internal execution sequence must be from start to end in order[Note A]So you can rest assured.

My practice is to add extra insurance to the code performance, highlighting the importance of order to prevent future attacks.

Namespace

PHP 5.2 or earlier versions have no namespace. Space delimiters are used in the code.\It will cause a parser error. Taking care of PHP 5.2 is actually easy to think about, just give up the namespace.

Note that PHP 5.3 + has a namespace. Whether it is calling CURLFile or usingclass_exists()To determine the existence of CURLFile, we recommend writing it\CURLFileSpecify the top-level space to prevent code packages from collapsing in the namespace.

All right, this trap is dug deep. just jump out and share it.

(The above solution is to repost the website. thank you for letting me find your article !)

Example (-experience): before curl of different versions, a collection tool is used to save the Collected Articles and images. the content of the articles is saved in the database...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.