您好,欢迎访问一九零五行业门户网

php curl采集站外内容示例代码

复制代码
3、通过正则匹配找到关键数据
$value){ //此处$value是数组,同时记录找到带匹配字符的整句和单独匹配的字符 echo '匹配到的整句:'.$value[0].''; echo '单独匹配到的:'.$value[1].''; }
复制代码
php curl的技巧1、超时的相关设置通过curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:curlopt_timeout 设置curl允许执行的最长秒数。 curlopt_timeout_ms 设置curl允许执行的最长毫秒数。 (在curl 7.16.2中被加入。从php 5.2.3起可使用。 )curlopt_connecttimeout 在发起连接前等待的时间,如果设置为0,则无限等待。 curlopt_connecttimeout_ms 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。 在curl 7.16.2中被加入。从php 5.2.3开始可用。 curlopt_dns_cache_timeout 设置在内存中保存dns信息的时间,默认为120秒。2、通过post提交数据,保留cookie
!extension_loaded('curl') && die('the curl extension is not loaded.');
$discuz_url = 'http://bbs.it-home.org';//论坛地址
$login_url = $discuz_url .'/logging.php?action=login';//登录页地址 $get_url = $discuz_url .'/my.php?item=threads'; //我的帖子 $post_fields = array();
//以下两项不需要修改 $post_fields['loginfield'] = 'username'; $post_fields['loginsubmit'] = 'true'; //用户名和密码,必须填写 $post_fields['username'] = 'jbxue'; $post_fields['password'] = '88888888'; //安全提问 $post_fields['questionid'] = 0; $post_fields['answer'] = ''; //@todo验证码 $post_fields['seccodeverify'] = ''; //获取表单formhash
$ch = curl_init($login_url); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 1); $contents = curl_exec($ch); curl_close($ch); preg_match('//i', $contents, $matches); if(!empty($matches)) { $formhash = $matches[1]; } else { die('not found the forumhash.'); } //post数据,获取cookie
$cookie_file = dirname(__file__) . '/cookie.txt'; //$cookie_file = tempnam('/tmp'); $ch = curl_init($login_url); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $post_fields); curl_setopt($ch, curlopt_cookiejar, $cookie_file); curl_exec($ch); curl_close($ch); //带着上面得到的cookie获取需要登录后才能查看的页面内容
$ch = curl_init($get_url); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 0); curl_setopt($ch, curlopt_cookiefile, $cookie_file); $contents = curl_exec($ch); curl_close($ch); var_dump($contents);
复制代码
其它类似信息

推荐信息