Think of PHP 5.0~5.6 version compatibility of the CURL file upload-dry "has been pit"

Source: Internet
Author: User
Tags deprecated
Consider the PHP 5.0~5.6 version of the compatibility of the CURL file upload-dry "has been pit"

Don't read PHP's official Chinese documentation! Version can't keep up with the pit dead you!

Different versions of PHP between curl differences

php Curl Support by giving curl_postfields passes associative arrays (rather than strings) to generate multipart/form-data .

Traditionally, PHP's curl is supported by the use of "@ + file full path "syntax append file for curl to read upload. This is consistent with the syntax for calling the Curl program directly on the command line:

curl_setopt (CH, curlopt_postfields, array  ( ' file '  = = . Realpath ( ' image.png ' ),)); equals$ curl-f  "File=@/absolute/path/to/image.png"  
     
    

but PHP introduced a new Curlfile class from 5.5 to point to the file. The Curlfile class can also define in detail the additional information that may appear in the Multipart/form-data data, such as MIME type, file name, and so on. PHP recommends using Curlfile instead of the old @ Syntax:

curl_setopt (CH, curlopt_postfields, [ ' file '  = = new  curlfile (Realpath ( ' Image.png ' )]); 

php 5.5 introduced in addition the curl_safe_upload option, you can force PHP's CURL module to reject the old @ syntax, accepts only curlfile-style files. The default value of 5.5 for the default value of false,5.6 is true.

But the point of the pit is: @ syntax has been deprecated in 5.5 and has been deleted directly in 5.6 (will produce erorexception:the usage of The? @filename ? API for file uploading is deprecated. Use the Curlfile class instead).

curl_safe_upload to False is . There is no literal sense of "set to False, you can open the old unsafe way"--the old way has been completely absent as an obsolete grammar. PHP 5.6+ = = Curlfile only, do not have any illusions.

My deployment environment is 5.4 (syntax only @ ), but the development environment is 5.6 (Curlfile only). It's not even on the 5.5. Both support the transition version, and the result is that two sets of code with environmental judgments must be written.

Now the question is coming ... (Excavator roll away!) )

Environmental judgment: Beware of magic numbers!

I've seen this code for environmental judgments:

if'5.4.0'0)

My comment on this code is only one word: excrement .

This judgment falls into a typical magic number trap . The version number is inexplicably in the code, not a half-day PHP manual and update history, it is difficult to understand the author is stuck in which function changes.

The code should return to its source. Our actual demand is actually: there are curlfile priority to use, no longer degenerate to the traditional @ grammar. So the code is here:

if(Class_exists (' \curlfile ')) {$field=Array(' fieldname '=New\curlfile (Realpath ($filepath)));}Else{$field=Array(' fieldname '='@'. Realpath ($filepath));}

Recommended for explicitly specified degradation options

from a reliable point of view, it is recommended to designate curl_safe_upload the value of to explicitly tell PHP whether to tolerate or prohibit the old @ the syntax. Note in the low version of PHP curlopt_safe_upload constant itself may not exist and needs to be judged:

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 The order of option settings

Regardless of whether the single-release curl_setopt() is curl_setopt_array() batch, the Curl option is always set to take effect one, and the SET option immediately affects the behavior of curl when setting subsequent options.

For exampleCURLOPT_SAFE_UPLOADJust andCURLOPT_POSTFIELDS's behavior. If you first setCURLOPT_POSTFIELDSAgain setCURLOPT_SAFE_UPLOAD, then the latter's binding effect will not take effect. Since the former is set, Curl has already finished reading the actual data!

Curl has a few options for this pit, so be careful. Fortunately, the existence of "dependency" of the options are not many, the mechanism is not complex, simple processing can be. My approach is to set all the options in bulk, and then curl_exec() use the single-shot setting until the first moment curl_setopt() CURLOPT_POSTFIELDS .

Actually incurl_setopt_array()In the array that is used to guaranteeCURLOPT_POSTFIELDSPosition is also reliable at the rear.The associative array of PHP is guaranteed in sequence., we can also assume thatcurl_setopt_array()The internal order of execution must be in order from beginning to end[注A], so be assured.

My approach is simply to add an excess of insurance to the code performance, highlighting the importance of order to prevent your hands from becoming cheap.

Name space

php 5.2 or below does not have a namespace. The code uses the space separator \ causes a parser error. To take care of PHP 5.2 is actually easy to think about, give up the namespace.

It is the PHP 5.3+ that has the namespace. Whether calling Curlfile or class_exists() judging the existence of curlfile, it is recommended to write \CURLFile explicitly designated top-level space to prevent the code from collapsing when it is wrapped within a namespace.

  • 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.