October 18, 2017-php curl get post request,-phpcurl

Source: Internet
Author: User

October 18, 2017-php curl get post request,-phpcurl

I. CURL Summary

I personally summarized the summary of curl requests into three steps.

1. Create a curl handle (curl_init) and set the parameter (curl_setopt) (enable the refrigerator)

2. execute the request (curl_exec) to process the returned data (put the elephant in)

3. Disable curl (curl_close) and release all resources (close the refrigerator)

In fact, if the code looks complicated, the complexity may be the logic for processing the returned data.

Ii. CURL_SETOPT

Therefore, the parameter name is "allow" and "SetOption" is used to set parameters, which include many parameters. Here we just extract some common parameters. To view more parameters, click here, common settings include UA, Cookie, and https.

Bool curl_setopt (resource $ ch, int $ option, mixed $ value) CURLOPT_USERAGENT contains a "User-Agent:" header string in the HTTP request. The content of CURLOPT_REFERER in the HTTP Request Header "Referer. The maximum number of seconds that CURLOPT_TIMEOUT allows cURL function execution. CURLOPT_RETURNTRANSFER TRUE returns the information obtained by curl_exec () as a string, rather than directly outputting it.
/* In the next two https requests, you must set */CURLOPT_SSL_VERIFYPEER FALSE to disable the cURL authentication peer certificate (peer's certificate ). The certificate to be verified can be set in the CURLOPT_CAINFO option or in the CURLOPT_CAPATH. (The default value is TRUE from cURL 7.10. From cURL 7.10, the installation is bound by default .) Set CURLOPT_SSL_VERIFYHOST to 1 to check whether a common name exists in the SSL Certificate of the server ). Note: The Common Name is generally the domain Name or sub-domain Name you want to apply for the SSL certificate ). If set to 2, the system checks whether the public name exists and matches the provided host name. 0 indicates the name not checked. In the production environment, this value should be 2 (default ). Value 1 is deleted in cURL 7.28.1.

The following uses the request for Baidu as an example. You need to set the URL, Ua, Cookie, and so on. Only SSL certificate verification is required for https requests. This is not required for http requests. If you need a regular request address, similar to example.com /? Id = $ I. Modify the for loop.

<?phpclass getRequest{    const sUA = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';    const sURL = 'https://www.baidu.com';    const sCookie = 'fake if you want';    function vInitRequest()    {        $curl = curl_init();        curl_setopt($curl, CURLOPT_HEADER, self::sUA);        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        /*         * ssl check,use for https url         */        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//        for ($iId = 1; $iId < 1000; $iId++) {//            $sURL = self::sURL.$iId;        curl_setopt($curl, CURLOPT_URL, self::sURL);        $this->sExecRequest($curl);//        }    }    function sExecRequest($curl)    {        $sRet = curl_exec($curl);        print_r($sRet);        /**         * handle your response         * stripos or preg         */        curl_close($curl);    }}$foo = new getRequest();$foo->vInitRequest();?>

 

Unfinished

 

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.