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 ;}