First, Curl Summary
Individual will summarize curl request into three steps
1. Create a curl handle (curl_init) and set the parameters (curl_setopt) (Open the refrigerator)
2, execute the request (CURL_EXEC), processing the returned data (put the elephant in)
3. Turn off Curl (curl_close) and release all resources (close the refrigerator)
In fact, if the code looks more complex, the complex place may be the logic of processing the returned data.
Second, curl_setopt
Therefore, the name Incredibles, SetOption set parameters, which include more parameters, here is just a few simple extraction, if you want to see more parameters, click here, common Settings ua, cookies, HTTPS, etc.
BOOL Curl_setopt (Resource $chInt$option,Mixed $value) curlopt_useragent contains an HTTP request with a"User-agent:"the string for the header. Curlopt_referer in the HTTP request header"Referer:"the content. Curlopt_timeout the maximum number of seconds that the CURL function is allowed to execute. Curlopt_returntransferTRUEThe information obtained by CURL_EXEC () is returned as a string, rather than as a direct output.
/* The following two re-HTTPS requests need to be set */curlopt_ssl_verifypeerFALSEDisable CURL Authentication Peer certificates certificate). The interchange certificate to be verified can be set in the Curlopt_cainfo option, or the certificate directory will be set in Curlopt_capath. (The default is TRUE from Curl 7.10.) The default binding installation starts with CURL 7.10. Curlopt_ssl_verifyhost is set to 1 to check if a common name exists in the server SSL certificate (common name). Note: The common name (Common name) is generally filled in with the domain name (domain) or subdomain (sub domain) where you will be applying for an SSL certificate. Set to 2, checks whether the common name exists and matches the host name provided. 0 is not checked for names. In a production environment, this value should be 2 (the default value). Support for value 1 was removed in CURL 7.28.1.
The following to request Baidu for example, need to use self-setting URL, Ua, cookies, HTTPS request only use SSL certificate check, HTTP request can not, if need to request a regular address, similar to example.com/?id= $i, modify for loop can.
<?PHPclassgetrequest{ConstSUA = ' mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;. NET CLR 1.1.4322) '; ConstsURL = ' https://www.baidu.com '; ConstSCookie = ' Fake if you want '; functionvinitrequest () {$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 <; $iId + +) {//$sURL = Self::surl. $iId;curl_setopt ($curl, Curlopt_url, Self::sURL); $this->sexecrequest ($curl);// } } functionSexecrequest ($curl) { $sRet= Curl_exec ($curl); Print_r($sRet); /** * Handle your response * Stripos or Preg*/Curl_close ($curl); }}$foo=Newgetrequest ();$foo-vinitrequest ();?>
Not finished
October 18, 2017-php Curl get POST request