Home >Backend Development >PHP Tutorial >Detailed explanation of PHP's CURL method curl_setopt() function case
This article mainly introduces PHP's CURL method curl_setopt() function cases: 1. Simple case of crawling web pages; 2. POST data case
The curl_setopt() function can be used to quickly and easily crawl web pages (Collection is very convenient lol), curl_setopt is an extension library of PHP
Conditions of use: It needs to be configured and enabled in php.ini. (PHP 4 >= 4.0.2)
//Uncomment the following
extension=php_curl.dll
InLinux Next, you need to recompile PHP. When compiling, you need to turn on the compilation parameters - add the "-with-curl" parameter to the configure command.
1. A simple case of web crawling:
[php] view plain copy print? // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/"); curl_setopt($ch, CURLOPT_HEADER, false); // 抓取URL并把它传递给浏览器 curl_exec($ch); //关闭cURL资源,并且释放系统资源 curl_close($ch);
2. POST data case:
[php] view plain copy print? // 创建一个新cURL资源 $ch = curl_init(); $data = 'phone='. urlencode($phone); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "http://www.post.com/"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 抓取URL并把它传递给浏览器 curl_exec($ch); //关闭cURL资源,并且释放系统资源 curl_close($ch);
##3. About SSL and Cookie
Regarding SSL, which is the HTTPS protocol, you only need to change http:// in the CURLOPT_URL connection to https://. Of course, there is also a parameter called CURLOPT_SSL_VERIFYHOST that can be set to verify the site.About Cookie, you need to understand the following three parameters:
[php] view plain copy print? /** * Make an HTTP request * * @return string API results * @ignore */ function http($url, $method, $postfields = NULL, $headers = array()) { $this->http_info = array(); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//让cURL自己判断使用哪个版本 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//在HTTP请求中包含一个"User-Agent: "头的字符串。 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//在发起连接前等待的时间,如果设置为0,则无限等待 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//设置cURL允许执行的最长秒数 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)输出 curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用后cURL将终止从服务端进行验证 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//第一个是cURL的资源句柄,第二个是输出的header数据 curl_setopt($ci, CURLOPT_HEADER, FALSE);//启用时会将头文件的信息作为数据流输出 switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); $this->postdata = $postfields; } break; case 'DELETE': curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($postfields)) { $url = "{$url}?{$postfields}"; } } if ( isset($this->access_token) && $this->access_token ) $headers[] = "Authorization: OAuth2 ".$this->access_token; $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR']; curl_setopt($ci, CURLOPT_URL, $url ); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers ); curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE ); $response = curl_exec($ci); $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); $this->url = $url; if ($this->debug) { echo "=====post data======\r\n"; var_dump($postfields); echo '=====info====='."\r\n"; print_r( curl_getinfo($ci) ); echo '=====$response====='."\r\n"; print_r( $response ); } curl_close ($ci); return $response; }
The above is the entire content of this article, I hope it will help everyone learn Helps.
Related recommendations:
The above is the detailed content of Detailed explanation of PHP's CURL method curl_setopt() function case. For more information, please follow other related articles on the PHP Chinese website!