Example of curlget/post in php (including https protocol)

Source: Internet
Author: User
In php, the curl function can implement get and post operations. we often use it for some human imitation operations. below I will briefly introduce the post and get examples. get method, the code is as follows: $ urlqu... in php, the curl function can implement get and post operations. we often use it for some human imitation operations. below I will briefly introduce the post and get examples.

The code for the get method is as follows:

$ Url = "http://www.phprm.com/index. php? A = B & c = d & e = f & g = ". urlencode ('Wang Lu's blog '); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // the result must be a string and be output to curl_setopt ($ ch, CURLOPT_HEADER, 0) on the screen; // do not use http header to accelerate the efficiency of curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ ch, CURLOPT_TIMEOUT, 15); $ output = curl_exec ($ ch ); curl_close ($ ch); var_dump ($ output );

Post method, the code is as follows:

$ Url = "http://www.phprm.com/index.php"; $ params = "a = B & c = d & e = f & g = ". urlencode ('Wang Lu's blog '); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // the result must be a string and be output to curl_setopt ($ ch, CURLOPT_HEADER, 0) on the screen; // do not use http header to accelerate the efficiency of curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ ch, CURLOPT_TIMEOUT, 15); // open source code phprm.com curl_setopt ($ ch, CURLOPT_POST, 1); // curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); $ output = curl_exec ($ ch); curl_close ($ ch); var_dump ($ output );

When requesting https data, the certificate is required. at this time, add the following two parameters to avoid ssl certificate check. the code is as follows:

Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https requests do not verify the certificate and hosts. curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );


Permanent link:

Reprint at will! Include the article address.

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.