Encapsulated PHP Curl Request function

Source: Internet
Author: User
This article mainly introduces the package of Php Curl request function, has a certain reference value, now share to everyone, the need for friends can refer to

After you turn on debug mode

How to use:

$params = [    ' name ' = = ' name '    ]; $url = '; HttpRequest ($url, ' POST ', $params, [],true);
/** * Curl Request * @param $url request URL address * @param $method request method get post * @param null $postfields post data array * @param array $heade RS Request header information * @param bool|false $debug Debug on default false * @return mixed */function HttpRequest ($url, $method = "GET", $POSTF    Ields = null, $headers = Array (), $debug = False) {$method = Strtoupper ($method);    $ci = Curl_init (); /* Curl Settings */curl_setopt ($CI, curlopt_http_version, CURL_HTTP_VERSION_1_0);//Version curl_setopt ($CI, Curlopt_user AGENT, "mozilla/5.0 (Windows NT 6.2; WOW64;    rv:34.0) gecko/20100101 firefox/34.0 ");//A String containing a" User-agent: "header in the HTTP request. curl_setopt ($ci, curlopt_connecttimeout, 60); /* The time to wait before initiating the connection, and if set to 0, wait indefinitely */curl_setopt ($CI, Curlopt_timeout, 7);    /* Set the maximum number of seconds that curl is allowed to execute */curl_setopt ($CI, Curlopt_returntransfer, true);//The information obtained by CURL_EXEC () is returned as a file stream, rather than as a direct output. Switch ($method) {case "POST": curl_setopt ($ci, Curlopt_post, true);//When enabled, a regular post request is sent with the type: application            /x-www-form-urlencoded, just like the form submitted. if (!Empty ($postfields)) {$tmpdatastr = Is_array ($postfields)? Http_build_query ($postfields): $postfields;            curl_setopt ($ci, Curlopt_postfields, $TMPDATASTR);        } break; Default:curl_setopt ($ci, Curlopt_customrequest, $method);    /*//Set the request mode */break; } $ssl = Preg_match ('/^https:\/\//i ', $url)?    True:false; curl_setopt ($ci, Curlopt_url, $url);//The URL address you need to get, or you can set the IF ($SSL) {curl_setopt ($ci, Curlopt_ssl_v) in the Curl_init () function Erifypeer, FALSE); The HTTPS request does not validate the certificate and the hosts curl_setopt ($ci, Curlopt_ssl_verifyhost, FALSE); Do not check for the existence of the SSL encryption algorithm from the certificate}//curl_setopt ($ci, Curlopt_header, true);    /* When enabled, the header file's information is output as a data stream */curl_setopt ($CI, curlopt_followlocation, 1); curl_setopt ($ci, Curlopt_maxredirs, 2);/* Specifies the maximum number of HTTP redirects, which is used with the curlopt_followlocation */curl_setopt ($CI,    Curlopt_httpheader, $headers);    curl_setopt ($ci, Curlinfo_header_out, true); /*curl_setopt ($ci, Curlopt_cookie, $CookiesTR);    * *cookie with past * */$response = curl_exec ($CI);    $requestinfo = Curl_getinfo ($CI);            if ($debug) {$html = <<

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.