PHP cURL initialization and execution method entry-level code, curl Initialization
This is the basis for data collection.
$ Ch = curl_init (); # Set the url and return the result. Whether to return the header curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // www.baidu.com/'); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ this-> ch, CURLOPT_HEADER, 1); # curl_setopt ($ this-> ch, CURLOPT_COOKIEJAR, $ cookie_file) for cookie files ); curl_setopt ($ this-> ch, CURLOPT_COOKIEFILE, $ cookie_file); # extra header curl_setopt ($ this-> ch, CURLOPT_HTTPHEADER, array ('user-Agent: mozilla/5.0 '); # Set postcurl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ poststring ); # connection and execution expiration time curl_setopt ($ this-> ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt ($ this-> ch, CURLOPT_TIMEOUT, 30 ); # Follow 301 302curl_setopt ($ this-> ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ this-> ch, CURLOPT_MAXREDIRS, 10); # refercurl_setopt ($ this-> ch, CURLOPT_REFERER, $ refer); # Set curl_setopt for http Version and port reuse ($ this-> ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt ($ this-> ch, forward, 1 ); # support httpscurl_setopt ($ this-> ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ this-> ch, CURLOPT_SSL_VERIFYHOST, 0); # If millisecond timeout is required, add: curl_setopt ($ this-> ch, CURLOPT_NOSIGNAL, 1); # Run $ response = curl_exec ($ ch); if (curl_errno ($ ch) {curl_error ($ ch ); exit ();} curl_close ($ ch );