免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3494 | 回复: 2
打印 上一主题 下一主题

优酷来疯直播间模拟登录群发信息 [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-07-10 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-09 11:11 |只看该作者 |倒序浏览
来疯直播间模拟登录,抓取正在直播的房间ID并群发信息,来疯首页http://www.laifeng.com/,代码注释每个方法前都写的很清楚,我想这么几行代码没办要每行都加注释吧?请大家注意口德
最底行有DEMO下载地址

[PHP]代码
  1. <?php

  2. //使用时必须先配置
  3. //$cookiefile(COOKIE 存放路径)
  4. //$user(用户名,优酷土豆来疯都行)
  5. //$pass(密码,优酷土豆来疯都行);

  6. class laifeng

  7. {

  8.     //cookie存放路径

  9.     private $cookiefile = "cookie.txt";

  10.     //房间号

  11.     private $id;

  12.     //用户名

  13.     private $user='1234@qq.com';

  14.     //密码

  15.     private $pass=12345678;

  16.     //初始化ID

  17.     public function __construct($idd=null){

  18.         $this->id=$idd;

  19.     }

  20.     //模拟登录获取yktk

  21.     public function getkey()

  22.     {

  23.         $user=urldecode($this->user);

  24.         $pass=urldecode(md5($this->pass));

  25.         $url = "http://www.laifeng.com/login/";

  26.         $data = "userName=".$user."&password=".$pass."&forever=1&returnurl=http%3a%2f%2fwww.laifeng.com%2f7437";

  27.         $ch = curl_init();

  28.         curl_setopt($ch, CURLOPT_URL, $url);

  29.         curl_setopt($ch, CURLOPT_HEADER, 0);

  30.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  31.         curl_setopt($ch, CURLOPT_POST, 1);

  32.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  33.         curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);

  34.         curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);

  35.         curl_setopt($ch, CURLOPT_REFERER, "http://www.laifeng.com/login/");

  36.         curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

  37.         $info = curl_exec($ch);

  38.         curl_close($ch);

  39.         $y = json_decode($info, true);

  40.         $yktk = $y['response'];

  41.         return $yktk;

  42.     }

  43.     //获取cookie

  44.     private function login(){

  45.         $yktk=$this->getkey();

  46.         if($yktk['code']!=0){

  47.             echo $yktk['msg'];

  48.             exit();

  49.         }else{

  50.             $url=$yktk['data'];

  51.             $ch=curl_init();

  52.             curl_setopt($ch,CURLOPT_URL,$url);

  53.             curl_setopt($ch,CURLOPT_HEADER,1);

  54.             curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);

  55.             curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  56.             curl_setopt($ch,CURLOPT_REFERER,"http://www.laifeng.com/".$this->id);

  57.             curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookiefile);

  58.             curl_exec($ch);

  59.             curl_close($ch);

  60.             echo("<script>location.reload();</script>");

  61.             return $yktk['msg'];

  62.             //echo $y[1];

  63.         }

  64.     }

  65.     //输出页面

  66.     public function curl(){

  67.         $this->login();

  68.         $url="http://www.laifeng.com/".$this->id;

  69.         $ch=curl_init();

  70.         curl_setopt($ch,CURLOPT_URL,$url);

  71.         curl_setopt($ch,CURLOPT_HEADER,0);

  72.         curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);

  73.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  74.         curl_setopt($ch,CURLOPT_REFERER,"http://www.laifeng.com/".$this->id);

  75.         curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookiefile);

  76.         $info=curl_exec($ch);

  77.         $info=str_replace('room/js','http://static.youku.com/ddshow/8125f00c/room/js',$info);

  78.         curl_close($ch);

  79.         return $info;

  80.     }

  81.      

  82.     //获取用户名

  83.     public function top(){

  84.         $url="http://www.laifeng.com";

  85.         $ch=curl_init();

  86.         curl_setopt($ch,CURLOPT_URL,$url);

  87.         curl_setopt($ch,CURLOPT_HEADER,1);

  88.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  89.         curl_setopt($ch,CURLOPT_REFERER,"http://www.laifeng.com/");

  90.         curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);

  91.         $m=curl_exec($ch);

  92.         curl_close($ch);

  93.         $mate="@<a class=\"user-name-link\" href=\"/my/profile/init\" target=\"_blank\" title=\"(.*)\">@iUs";

  94.         preg_match_all($mate,$m,$arr);

  95.         if(@$arr[1][0]){

  96.             echo $arr[1][0];

  97.         }else{

  98.             echo("<script>alert('登录超时,正在尝试自动登录.....');</script>");

  99.             echo $this->login();

  100.         }

  101.          

  102.     }

  103.     //发送信息模板

  104.     public function msg($msg,$url){

  105.         //$url="http://www.laifeng.com/room/7437/chat/save";

  106.         $ch=curl_init();

  107.         curl_setopt($ch,CURLOPT_URL,$url);

  108.         curl_setopt($ch,CURLOPT_HEADER,0);

  109.         curl_setopt($ch,CURLOPT_POST,1);

  110.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  111.         curl_setopt($ch,CURLOPT_REFERER,"http://www.laifeng.com/".$this->id);

  112.         curl_setopt($ch,CURLOPT_POSTFIELDS,$msg);

  113.         curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);

  114.         $info=curl_exec($ch);

  115.         curl_close($ch);

  116.         $arr=json_decode($info,true);

  117.         if($arr['response']['code']==-2){

  118.             echo("<script>alert('登录超时,正在尝试自动登录.....');</script>");

  119.             echo $this->login();

  120.         }else{

  121.           echo $arr['response']['msg'];  

  122.         }

  123.     }

  124.     //发送文字 参数为发送内容

  125.     public function msgtxt($data){

  126.         $msg="single=0&content=".urlencode($data)."&anchorId=0&roomId=".$this->id;

  127.         $url="http://www.laifeng.com/room/".$this->id."/chat/save";

  128.         return $this->msg($msg,$url);

  129.     }

  130.      

  131.     //送花  参数为数量

  132.     public function flower($data=1){

  133.         $msg="roomId=".$this->id."&quantity=".$data;

  134.         $url="http://www.laifeng.com/room/".$this->id."/star/send";

  135.         return $this->msg($msg,$url);

  136.     }

  137.     //抓取正在直播的ID

  138.      

  139. }//laifeng class

  140. class exe{

  141.     //抓取房间ID

  142.     public function anchor($pageid=1){

  143.         $page=file_get_contents("http://www.laifeng.com/anchor/search?pageNo=".$pageid);

  144.         //$count=file_get_contents("http://www.laifeng.com/anchor/search");

  145.         $mage="@<p class=\"name\">(.*)<a href=\"/(.*)\" title=\"(.*)\" target=\"_blank\">(.*)</a>@iUs";

  146.         $match="@//总页数(.*)totalNum:(.*),(.*)//当前页数@iUs";

  147.         preg_match_all($mage,$page,$arr);

  148.         preg_match_all($match,$page,$pagecount);

  149.         $page=array();

  150.         $page['count']=$pagecount[2][0];

  151.         $page['data']=$arr[2];

  152.         return $page;

  153.     }

  154.     //抓取房间总页数

  155.     public function pagecount(){

  156.         $page=file_get_contents("http://www.laifeng.com/anchor/search");

  157.         $match="@//总页数(.*)totalNum:(.*),(.*)//当前页数@iUs";

  158.         preg_match_all($match,$page,$arr);

  159.         echo $arr[2][0];

  160.     }

  161. }

  162. //$exe=new exe();

  163. //$exe->pagecount();

  164. //$arr=$exe->anchor(1);

  165. //foreach($arr as $k=>$v){

  166. //    $lai=new laifeng($v);

  167. //    echo"房间ID:".$v."&nbsp;";

  168. //    echo $lai->msgtxt('嗨');

  169. //    echo"<br />";

  170. //}



  171. //echo $lai->msgtxt("这是一段测试文字1234");

  172. //for($i=7437;$i<=7439;$i++){

  173. //    $lai=new laifeng($i);

  174. //   echo $lai->msgtxt("1111");

  175. //}



  176. //$lai->flower(1);

  177. //echo $lai->curl();

  178. ?>
复制代码

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
2 [报告]
发表于 2015-07-10 09:49 |只看该作者
赞!

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2015-07-10 09:56 |只看该作者
不错。楼主从哪整来的源码。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP