This article mainly introduces the entry-level code of PHPcURL initialization and execution method. This article provides a sample code that contains detailed comments. if you need it, you can refer to it as a collection basis.
$ 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); 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 );