Http request encapsulation example implemented by php and php request encapsulation example

Source: Internet
Author: User

Http request encapsulation example implemented by php and php request encapsulation example

This example describes the http request encapsulation of php. We will share this with you for your reference. The details are as follows:

/*** HTTP request sending method, currently, only CURL requests can be sent * @ param string $ url request URL * @ param array $ params request parameter * @ param string $ method Request method GET/POST * @ return array $ data response data */protected function http ($ url, $ params, $ method = 'get', $ header = array (), $ multi = false) {$ opts = array (CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $ header) ;/* Set specific parameters based on the request type */switch (strtoupper ($ method) {case 'get': $ opts [CURLOPT_URL] = $ url. '&'. http_build_query ($ params); dump ($ opts [CURLOPT_URL]); break; case 'post': // determine whether to transfer files $ params = $ multi? $ Params: http_build_query ($ params); $ opts [CURLOPT_URL] = $ url; dump ($ opts [CURLOPT_URL]); $ opts [CURLOPT_POST] = 1; $ opts [CURLOPT_POSTFIELDS] = $ params; break; default: throw new Exception ('unsupported request method! ');}/* Initialize and execute the curl Request */$ ch = curl_init (); curl_setopt_array ($ ch, $ opts); $ data = curl_exec ($ ch ); $ error = curl_error ($ ch); curl_close ($ ch); if ($ error) throw new Exception ('request error :'. $ error); return $ data ;}

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.