PHP's Curlopt_postfields parameter uses the difference between the array and the string

Source: Internet
Author: User
Tags curl ftp http cookie http post

The Curl component of PHP is a very common HTTP request emulator.

Usually when I send post data, I'm used to writing this:
curl_setopt ($ch, Curlopt_postfields, $post _data);
But when a request is sent to a server, the server returns 500. and use the socket to connect to send the request, return the normal data.

Attempt to send to a <?php print_r only ($_server);? > on the Web page, you can see that when you use an array to send post data, you receive the following content_type:

The code is as follows Copy Code

[Content_Type] => multipart/form-data; boundary= —————————-f924413ea122
and using Http_build_query ($post _data), the Content_Type received are as follows:
[Content_Type] => application/x-www-form-urlencoded

Visible, HTTP headers send content_type:application/x-www-form-urlencoded when Curlopt_postfields is set to an array. This is the normal Web page <form> submit the form when the browser sends the head. And Multipart/form-data We know this is a form for uploading files. Includes the boundary delimiter, which can be a lot more bytes.

The manual mentions:

The full data to post in a HTTP "post" operation. To post a file, prepend a filename with @ and use the full path. This can either is passed as a urlencoded string like ' Para1=val1&para2=val2& ... ' or as an array with the field name As key and field data as value. If value is a array, the Content-type header would be set to Multipart/form-data.

When using an array to provide post data, the Curl component is probably designed to be compatible with @filename this upload file, by default the Content_Type is set to Multipart/form-data. Although it does not affect most Web servers, there are a few servers that are not compatible.

The conclusion of this paper is that, in the absence of the need to upload the file, try to http_build_query the post submitted data, and then send out, can achieve better compatibility, smaller request packets.


the values of the following options will be used as long reshaping (specified in the option argument):


curlopt_infilesize: When you upload a file to a remote site, this option tells PHP the size of the file you uploaded.
Curlopt_verbose: If you want to curl report every unexpected thing, set this option to a value other than 0.
Curlopt_header: If you want to include a header in the output, set this option to a value other than 0.
curlopt_noprogress: If you don't have PHP to display a process bar for curl transmissions, set this option to a non 0 value. Note: PHP automatically sets this option to a value other than 0, and you should only change this option for debugging purposes.
Curlopt_nobody: If you don't want to include the body part in the output, set this option to a value other than 0.
Curlopt_failonerror: If you want PHP to be in error (HTTP code returns greater than or equal to 300), do not display, set this option to a person not 0 value. The default behavior is to return a normal page, ignoring the code.
curlopt_upload: If you want PHP to be ready for upload, set this option to a value other than 0.
Curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a non 0 value. This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.
curlopt_ftplistonly: Set this option to a value other than 0, PHP will list the list of directory names for FTP.
curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.
CURLOPT_NETRC: Set this option to a value other than 0, PHP will find the username and password of the remote site you want to connect to in your ~./netrc file.
curlopt_followlocation: Set this option to a non 0 value (like "Location:"), the server will send it as a part of the HTTP header (note that this is recursive, PHP will send the shape as "Location:" the head).
curlopt_put: Set this option for a non 0 value to upload a file with HTTP. To upload this file, you must set the Curlopt_infile and Curlopt_infilesize options.
Curlopt_mute: Set this option to a value other than 0, PHP will be completely silent for the curl function.
curlopt_timeout: Sets a long shaping number, as the maximum continuation of how many seconds.
curlopt_low_speed_limit: Sets a long shaping number to control how many bytes are transferred.
Curlopt_low_speed_time: Set a long shaping number, control how many seconds to transfer curlopt_low_speed_limit The specified number of bytes.
Curlopt_resume_from: Passes a long shaping parameter that contains the byte offset address (the start form you want to transfer to).
curlopt_sslversion: Passes a long parameter containing the SSL version. The default PHP will be determined by its own efforts, and in more security you must set it manually.
curlopt_timecondition: Passes a long parameter specifying how to handle the Curlopt_timevalue parameter. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince. This is for HTTP only.
Curlopt_timevalue: Passes a number of seconds from 1970-1-1 to the present. This time is used by the Curlopt_timevalue option as the specified value or by default timecond_ifmodsince.

The values of the following options will be used as strings:

Curlopt_url: This is the URL you want to retrieve with PHP. You can also set this option when initializing with the Curl_init () function.
curlopt_userpwd: Pass a string in a form like [Username]:[password] style, the function of PHP to connect.
curlopt_proxyuserpwd: Passes a string in a form like [Username]:[password] to connect to the HTTP proxy.
Curlopt_range: Pass a range that you want to specify. It should be "XY" format, X or Y is excepted. HTTP transmissions also support several intervals, separated by a x-y,n-m.
Curlopt_postfields: Passes a string of all data that is an HTTP "POST" operation.
curlopt_referer: Contains a string of "REFERER" headers in the HTTP request.
curlopt_useragent: Contains a string of "user-agent" headers in the HTTP request.
curlopt_ftpport: Passes an IP address that contains the FTP "POST" instruction. This post instruction tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under Unix), or '-' (using the system default IP address).
Curlopt_cookie: Passes a header connection that contains an HTTP COOKIE.
Curlopt_sslcert: Passes a string containing the PEM format certificate.
curlopt_sslcertpasswd: Passes a password that is required to use the Curlopt_sslcert certificate.
Curlopt_cookiefile: A string that passes the name of a file that contains cookie data. This cookie file can be in Netscape format, or in the HTTP style header that is stockpiled in the file.
Curlopt_customrequest: When making an HTTP request, passing a character is used by get or head. It is useful for delete or other operations, more pass a string of used instead of a and head when doing a HTTP request. This is useful to doing or another, more obscure, HTTP request. Note: Do not do this before confirming your server support command. The following options require a file description (obtained by using the fopen () function):
curlopt_file: This file will be the output file where you put the transfer, and the default is stdout.
Curlopt_infile: This file is the input file you sent over.
Curlopt_writeheader: This file is written with the head part of your output.
Curlopt_stderr: This file is written with errors rather than STDERR. To get an example of a page that needs to be logged in, the current practice is to log in once every time, and the person who needs it will improve.

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.