首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
curl保存cookie,https采集要用到函数 取两个串之间内容 替换两个串之间内容
编辑
代码正文
双击正文可选择全部
1[代码][php]
尝试一下
<?php $debug=1; $debug ? error_reporting(E_ALL ^ E_NOTICE) : error_reporting(0); @date_default_timezone_set('Etc/GMT-8'); echo getSourcesHttps('http://218.249.164.250:2048/login','http://218.249.164.250:2048/menu',"user=20070017&pass=a147258369&url=^U"); echo '<script type="text/javascript">setTimeout(\'location.href="http://218.249.164.250:2048/login?url=http://www.cnki.net"\',3000);</script>'; /** * 抓取一个页面 * @param str $url= "https://cn-virtual.excalibur.com.hk:5512/spadmin/english/validate.php"; 需要登陆验证的页面地址 * @param str $get_content_url 采集登陆后的页面 * @param str $post_fields= "username=FENGKONG&password=368382368382"; post串 * @param str $User_Agent = "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"; user_agent信息 如“baiduspider”或“googlebot” * @param str $Referer_Url = 'http://www.baidu.com/'; 来源页 */ function getSourcesHttps($Url='http://218.249.164.250:2048/login',$get_content_url='http://218.249.164.250:2048/menu',$post_fields= "user=20070017&pass=a147258369&url=^U",$User_Agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',$Referer_Url='https://www.baidu.com/') //抓取某个指定的页面 { $cookie_file=tempnam('./temp','cookie'); $ch = curl_init($Url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent); curl_setopt($ch, CURLOPT_REFERER, $Referer_Url); #curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); #curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); $MySources1 = curl_exec ($ch); curl_close($ch); $url=$get_content_url; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent); curl_setopt($ch, CURLOPT_REFERER, $Url); #curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); #curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $MySources2 = curl_exec($ch); curl_close($ch); return $MySources2; } /** * 取两个串之间内容 如参数一个为空返回为空 * @param str first str; * @param str second str; * @param str haystack; 其中没有first或second 返回空 * @return str 成功返回串,失败返回空串 */ function sub_getstr($f,$s,$h) { if(empty($f) ||empty($s) ||empty($h)) return ''; list($part1,$part2)=explode($f,$h,2); empty($part2) ? $get='' : list($get,$tem)=explode($s,$part2,2); if($get===$part2) $get=''; return empty($get) ? '' : $get; } /** * 替换两个串之间内容 把1 2之间内容换成 3 * @param str first str; * @param str second str; * @param str repalce str; * @param str haystack; * @return str 成功返回串,失败返回空串 */ function sub_replace($f,$s,$r,$h) { return str_replace(sub_getstr($f,$s,$h),$r,$h); } /** * 正则取一个串 * @param str $pattern '/a(b)c/' * @return str */ function preg_get($pattern,$file) { preg_match($pattern,$file,$matches); return $matches; } /** * 正则取多个串 返回所有匹配 * @param str $pattern '/a(b)(c)d/' * @return str */ function preg_get_all($pattern,$file) { preg_match_all($pattern,$file,$matches); return $matches; }
文明上网理性发言,请遵守新闻评论服务协议
0 条评论
发布评论
全部评论
最新
/
最热
暂无评论
加载更多
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 3 个查询,用时 0.0018341541290283 秒,PHP脚本用时 0.003888 秒,占用内存 0.508 MB,Gzip 已启用