- curl_close — 关闭一个cURL会话
- curl_copy_handle — 复制一个cURL句柄和它的所有选项
- curl_errno — 返回最后一次的错误号
- curl_error — 返回一个保护当前会话最近一次错误的字符串
- curl_escape — 使用 URL 编码给定的字符串
- curl_exec — 执行一个cURL会话
- curl_file_create — 创建一个 CURLFile 对象
- curl_getinfo — 获取一个cURL连接资源句柄的信息
- curl_init — 初始化一个cURL会话
- curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄
- curl_multi_close — 关闭一组cURL句柄
- curl_multi_exec — 运行当前 cURL 句柄的子连接
- curl_multi_getcontent — 如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流
- curl_multi_info_read — 获取当前解析的cURL的相关传输信息
- curl_multi_init — 返回一个新cURL批处理句柄
- curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
- curl_multi_select — 等待所有cURL批处理中的活动连接
- curl_multi_setopt — 为 cURL 并行处理设置一个选项
- curl_multi_strerror — Return string describing error code
- curl_pause — Pause and unpause a connection
- curl_reset — Reset all options of a libcurl session handle
- curl_setopt_array — 为cURL传输会话批量设置选项
- curl_setopt — 设置一个cURL传输选项
- curl_share_close — Close a cURL share handle
- curl_share_init — Initialize a cURL share handle
- curl_share_setopt — Set an option for a cURL share handle.
- curl_strerror — Return string describing the given error code
- curl_unescape — 解码给定的 URL 编码的字符串
- curl_version — 获取cURL版本信息
1、一个简单的例子
<?php // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, false); // 抓取URL并把它传递给浏览器 curl_exec($ch); //关闭cURL资源,并且释放系统资源 curl_close($ch); ?>
2、head信息的设置
$header [] = ‘Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘; $header [] = ‘Accept-Encoding:gzip, deflate‘; $header [] = ‘Accept-Language:en‘; $header [] = ‘Cache-Control:max-age=0‘; $header [] = ‘Connection:keep-alive‘; $header [] = ‘Cookie: PHPSESSID=jkuflj1jgoo7vvh5fcae3md262; qtrans_cookie_test=1‘; $header [] = ‘DNT:1‘; $header [] = ‘CLIENT-IP:58.68.44.61‘; $header [] = ‘X-FORWARDED-FOR:58.68.44.61‘; //$header [] = ‘Host:www.baidu.com‘; $header [] = ‘If-Modified-Since:Tue, 08 Sep 2015 03:07:27 GMT‘; $header [] = ‘User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0‘;
3、setopt信息的设置
$cookie = tempnam(‘/data/www/tmp/‘,‘seek‘); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_AUTOREFERER,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); if($type == ‘POST‘ && !empty($data)){ //POST && is not empty $data curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_ENCODING, ‘gzip,deflate‘); curl_setopt($ch, CURLOPT_TIMEOUT,120); $content = curl_exec($ch); curl_close($ch);
4、文件保存
$tp = @fopen($path, ‘a‘); fwrite($tp, $content); fclose($tp);
5、登录ftp
<?php $username = ‘username‘; $password = ‘password‘; $url = ‘example.com‘; $ftp_server = "ftp://" . $username . ":" . $password . "@" . $url; echo "Starting CURL.\n"; $ch = curl_init(); echo "Set CURL URL.\n"; //curl FTP curl_setopt($ch, CURLOPT_URL, $ftp_server); //For Debugging //curl_setopt($ch, CURLOPT_VERBOSE, TRUE); //SSL Settings curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); //List FTP files and directories curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE); //Output to curl_exec curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); echo "Executing CURL.\n"; $output = curl_exec($ch); curl_close($ch); echo "Closing CURL.\n"; echo $output . "\n"; $files = explode("\n", $output); print_r($files); ?>
5、使用代理
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ‘http://www.baidu.com‘); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, ‘proxy.lxvoip.com:1080‘); curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘user:password‘); $data = curl_exec(); curl_close($ch); ?>
一些参数说明
1、启用调试
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
2、ssl设置
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); or curl_setopt($ch, CURLOPT_VERBOSE, ‘1‘); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ‘2‘); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ‘1‘); curl_setopt($ch, CURLOPT_CAINFO, getcwd().‘/cert/ca.crt‘); curl_setopt($ch, CURLOPT_SSLCERT, getcwd().‘/cert/mycert.pem‘); curl_setopt($ch, CURLOPT_SSLCERTPASSWD, ‘password‘);
3、session
$cookie = tempnam(‘/data/www/tmp/‘,‘seek‘); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
4、参数解释
CURLOPT_AUTOREFERER 当根据Location:重定向时,自动设置header中的Referer:信息 CURLOPT_FOLLOWLOCATION 启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量 CURLOPT_HEADER 启用时会将头文件的信息作为数据流输出(保存图片的时候要关闭) CURLOPT_NOBODY 启用时将不对HTML中的BODY部分进行输出 CURLOPT_POST 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样 CURLOPT_RETURNTRANSFER 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出 CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数 CURLOPT_COOKIE 设定HTTP请求中"Cookie: "部分的内容。多个cookie用分号分隔,分号后带一个空格(例如, "fruit=apple; colour=red") CURLOPT_COOKIEFILE 包含cookie数据的文件名,cookie文件的格式可以是Netscape格式,或者只是纯HTTP头部信息存入文件 CURLOPT_COOKIEJAR 连接结束后保存cookie信息的文件 CURLOPT_ENCODING HTTP 请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型 CURLOPT_POSTFIELDS 全部数据使用HTTP协议中的"POST"操作来发送 CURLOPT_HTTPHEADER 一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置: array(‘Content-type: text/plain‘, ‘Content-length: 100‘)
参考地址 : http://php.net/manual/zh/function.curl-setopt.php
时间: 2024-10-20 11:20:34