: This article mainly introduces the curl request method. if you are interested in the PHP Tutorial, refer to it.
/*** 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 */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);/* based on the request type Set specific parameters */switch (strtoupper ($ method) {case 'get': $ opts [CURLOPT_URL] = $ url .'? '. Http_build_query ($ params); break; case 'Post': // determines whether a file is transmitted. // $ params = $ multi? $ Params: http_build_query ($ params); $ opts [CURLOPT_URL] = $ 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 ;}
The above introduces the curl request method, including some content, hope to be helpful to friends who are interested in the PHP Tutorial.