Home php教程 PHP开发 Usage details of CURLOPT_POSTFIELDS parameter of CURL in PHP

Usage details of CURLOPT_POSTFIELDS parameter of CURL in PHP

Dec 23, 2016 pm 03:18 PM

Under normal circumstances, when we use CURL to submit POST data, we are used to writing it like this:

curl_setopt( $ch, CURLOPT_POSTFIELDS,$post_data);
Copy after login

But this writing method is not very useful sometimes, and may get a 500 error returned by the server. But when we try to use Socket to submit data to the server, we will get very correct results.
For example, we have the following PHP file on the server:

<?php print_r($_SERVER);?>
Copy after login

When we use CURL to send some data to the server without paying attention to details, we may get the following results, which is not our ideal result:

[CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122
Copy after login

But if we use http_build_query($post_data) instead of $post_data and then submit data to this PHP script, we will get different results from the above. This is our ideal result:

[CONTENT_TYPE] => application/x-www-form-urlencoded
Copy after login

From above It is not difficult to see from this example that when using CURL and the parameter is data, when submitting data to the server, the HTTP header will send Content_type: application/x-www-form-urlencoded. This is the header sent by the browser when submitting the form on a normal web page

. And multipart/form-data we know this is the form used to upload files. Including the boundary delimiter will add many bytes.
The official manual says this:

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 be passed as a urlencoded string like ‘para1=val1¶2=val2&…&#39; or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
Copy after login

When using an array to provide post data, the CURL component is probably to be compatible with the @filename way of writing uploaded files. By default, the content_type is set to multipart/form-data. Although it has no impact on most servers, there are still a few servers that are incompatible.
After some summary, we finally came to the conclusion: when there is no need to upload files, try to perform http_build_query processing on the data submitted by post and then send it out, which can achieve better compatibility and smaller request data packets.


For more details on the use of CURLOPT_POSTFIELDS parameters of CURL in PHP, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)