PHP implementation of HTTP request Encapsulation example

Source: Internet
Author: User
Tags php programming
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.

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.