A simple PHP curl encapsulation function supporting https

Source: Internet
Author: User

Update

Please refer to the latest article "using PHP curl to access HTTPS"

Bytes --------------------------------------------------------------------------------------------------------------------

The title is a bit long. It is actually used to post data to the https server.

Function curlpost ($ URL, $ data, $ timeout = 30) <br/>{< br/> $ SSL = substr ($ URL, 0, 8) = "https: //"? True: false; <br/> $ CH = curl_init (); <br/> $ opt = array (<br/> curlopt_url => $ URL, <br/> curlopt_post => 1, <br/> curlopt_header => 0, <br/> curlopt_postfields => (array) $ data, <br/> curlopt_returntransfer => 1, <br/> curlopt_timeout => $ timeout, <br/>); <br/> if ($ SSL) <br/>{< br/> $ OPT [curlopt_ssl_verifyhost] = 1; <br/> $ OPT [curlopt_ssl_verifypeer] = false; <br/>}< br/> curl_setopt_array ($ ch, $ OPT); <br/> $ DATA = curl_exec ($ ch ); <br/> curl_close ($ ch); <br/> return $ data; <br/>}< br/> $ DATA = curlpost ('https: // ssl.test.com ', array ('p' => 'hello'); <br/> echo ($ data );

----------------------------- I am a split line --------------------------------

In fact, this tells the server not to perform SSL authentication and does not actually Use https.

To Use https, you must provide a CA certificate.

The preceding settings for SSL are as follows:

Set curlopt_ssl_verifypeer to true. This indicates that SSL certificate authentication is performed. Set curlopt_ssl_verifyhost to 2. Set curlopt_cainfo to the certificate path.

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.