X-www-form-urlencoded and Multipart/form-data method Post submission data in PHP curl

Source: Internet
Author: User
Tags curl

Multipart/form-data Way

Post's Curl library, simulate post submission, the default way Multipart/form-data, this is a post submission of several basic implementation methods.

$POSTURL = ';
$postData = Array (
' user_name ' => $userName,
' Identity_no ' => $idCardNo
);

$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $POSTURL);
curl_setopt ($curl, Curlopt_useragent, ' opera/9.80 (Windows NT 6.2; Win64; x64) presto/2.12.388 version/12.15 ');
curl_setopt ($curl, Curlopt_ssl_verifypeer, false); Stop Verifying certificate
curl_setopt ($curl, Curlopt_returntransfer, true);
curl_setopt ($curl, Curlopt_post, true);
curl_setopt ($curl, Curlopt_postfields, $postData);
curl_setopt ($curl, curlopt_followlocation, true);
$r = curl_exec ($curl);
Curl_close ($curl);

Print_r ($R);

If you want to use it, you can try it directly.

X-www-form-urlencoded Way

It's quite handy to post a PHP curl library. However, different submissions, ContentType different causes your API can receive data is also a variable, here to a simple example.

$POSTURL = ';
$postData = Array (
' user_name ' => $userName,
' Identity_no ' => $idCardNo
);
$postData = Http_build_query ($postData);
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $POSTURL);
curl_setopt ($curl, Curlopt_useragent, ' opera/9.80 (Windows NT 6.2; Win64; x64) presto/2.12.388 version/12.15 ');
curl_setopt ($curl, Curlopt_ssl_verifypeer, false); Stop Verifying certificate
curl_setopt ($curl, Curlopt_returntransfer, true);
curl_setopt ($curl, Curlopt_post, true);
curl_setopt ($curl, Curlopt_httpheader, Array (' content-type:application/x-www-form-urlencoded '));
curl_setopt ($curl, Curlopt_postfields, $postData);
curl_setopt ($curl, curlopt_followlocation, true);
$r = curl_exec ($curl);
Curl_close ($curl);

Print_r ($R);

The key piece of code is

Curl_setopt ($curl, Curlopt_httpheader, Array (' content-type:application/x-www-form-urlencoded '));

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.