The example in this article describes the PHP HTTP request encapsulation. Share to everyone for your reference, as follows:
/*** send HTTP request method, currently only support curl Send request * @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 = Curlopt_returntransfer = 1, Curlopt_ssl_verifypeer = False, Curlopt_ssl_v Erifyhost = False, Curlopt_httpheader = $header); /* Set specific parameters according to the request type */switch (Strtoupper ($method)) {case ' GET ': $opts [curlopt_url] = $url. ' & '. Http_build_query ($params); Dump ($opts [Curlopt_url]); Break Case ' POST '://Determine if file transfer $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 mode! '); }/* Initializes and performs a 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;}
I hope this article is helpful to you in PHP programming.