PHP实现QQ空间自动回复说说的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP实现QQ空间自动回复说说的方法。分享给大家供大家参考,具体如下:

<?php 
header("Content-type: text/html; charset=utf-8"); 
$qq = '';//QQ号 
$sid = '';//填写sid的值 
$con = '';//自定义内容,留空则使用simsimi 
$qzone = new qzone($qq,$sid); 
class qzone{ 
  private $sid =''; 
  public function __construct($qq,$sid){ 
    $this->sid = $sid; 
    $url = "http://ish.z.qq.com/infocenter_v2.jsp?B_UID={$qq}&sid={$sid}&g_ut=2"; 
    $re = $this->fetch($url); 
    $this->getsaying($re); 
  } 
  private function fetch($url,$postdata=null){ 
    $ch = curl_init();//www.oicqzone.com 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 820)"); 
    if($postdata!=null) curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
    $re = curl_exec($ch); 
    curl_close($ch); 
    return $re; 
  } 
  private function getsaying($html){ 
    preg_match_all('/<\/a>:(.*?)评论\(\d\)<\/a>/',$html,$match); 
    foreach($match[0] as $k){ 
      if(strstr($k,'评论(0)')){ 
        $k = str_replace(PHP_EOL, '', $k); 
        $k = str_replace('
', '', $k); 
        $k = html_entity_decode($k); 
        preg_match('/<\/a>:(.*?)<span class="txt-fade">/',$k,$content);//www.oicqzone.com 
        preg_match('/myfeed_mood.jsp\?sid=.*&B_(.*?)&t1_source/',$k,$data); 
        $content = preg_replace('/<img[^>]+>/', '', $content[1]); 
        echo '找到一条说说:'.$content.' 机器人的回复是:'; 
        $data = 'B_'.$data[1]; 
        $content = talk($content); 
        echo $content.'<br />'; 
        $this->postcomment($content,$data); 
        sleep(3); 
      } 
    } 
  } 
  private function postcomment($content,$data){ 
    $postdata = "content={$content}&{$data}&t1_source=1&feedcenter_pn=1&flag=1&type=all&channel=0&back=false&offset=0&ic=false&dl=null&to_tweet=0&submit=%E8%AF%84%E8%AE%BA"; 
    $this->fetch("http://blog30.z.qq.com/mood/mood_reply.jsp?sid={$this->sid}&g_ut=2",$postdata);   
  } 
} 
function talk($content){ 
    global $con; 
    if($con) return $con; 
  $content = str_replace(' ', '', $content); 
    $ch = curl_init(); 
  curl_setopt($ch,CURLOPT_URL,'http://www.simsimi.com/talk.htm'); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_HEADER,1); 
  curl_setopt($ch, CURLOPT_NOBODY, false); 
  $rs = curl_exec($ch); 
  preg_match_all('/Set-Cookie: (.+)=(.+)$/m', $rs, $regs); 
  foreach($regs[1] as $i=>$k); 
  $cc=str_replace(' Path','' ,$k); 
  $cc='simsimi_uid=507454034223;'.$cc; 
  $re = HTTPClient('http://www.simsimi.com/func/reqN?lc=ch&ft=1.0&req='.$content.'&fl=http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm',$cc); 
  $re = json_decode($re,true); 
  return $re['sentence_resp']; 
} 
function HTTPClient($url,$cookie){ 
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL,$url); 
  curl_setopt($ch, CURLOPT_COOKIE,$cookie); 
  curl_setopt($ch, CURLOPT_HEADER, 0); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
  $re = curl_exec($ch); 
  curl_close($ch); 
  return $re; 
} 
?>

希望本文所述对大家PHP程序设计有所帮助。

相关文章

Window下PHP三种运行方式图文详解

Window下PHP三种运行方式图文详解

PHP能不能成功的在Apache服务器上运行,就看我们如何去配置PHP的运行方式。PHP运行目前为止主要有三种方式: a、以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成...

php采用curl访问域名返回405 method not allowed提示的解决方法

/** * http测试 * 注:PHP版本5.2以上才支持CURL_IPRESOLVE_V4 * @param $url 网站域名 * @param $type 网站访问协...

php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天...

PHP OPCode缓存 APC详细介绍

PHP OPCode缓存 APC详细介绍

APC的介绍 The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is t...

php中防止SQL注入的最佳解决方法

如果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子:复制代码 代码如下:$unsafe_variable = $_POST['user_in...