首頁 >後端開發 >php教程 >PHP基於curl呼叫自動回覆接口

PHP基於curl呼叫自動回覆接口

墨辰丷
墨辰丷原創
2018-06-05 17:37:481658瀏覽

本篇文章主要介紹PHP基於curl呼叫自動回覆接口,有興趣的朋友參考下,希望對大家有幫助。

程式碼如下:

<?php 
header("Content-type: text/html; charset=utf-8"); 
$qq = &#39;&#39;;//QQ号 
$sid = &#39;&#39;;//填写sid的值 
$con = &#39;&#39;;//自定义内容,留空则使用simsimi 
$qzone = new qzone($qq,$sid); 
class qzone{ 
  private $sid =&#39;&#39;; 
  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(&#39;/<\/a>:(.*?)评论\(\d\)<\/a>/&#39;,$html,$match); 
    foreach($match[0] as $k){ 
      if(strstr($k,&#39;评论(0)&#39;)){ 
        $k = str_replace(PHP_EOL, &#39;&#39;, $k); 
        $k = str_replace(&#39;
&#39;, &#39;&#39;, $k); 
        $k = html_entity_decode($k); 
        preg_match(&#39;/<\/a>:(.*?)<span class="txt-fade">/&#39;,$k,$content);//www.oicqzone.com 
        preg_match(&#39;/myfeed_mood.jsp\?sid=.*&B_(.*?)&t1_source/&#39;,$k,$data); 
        $content = preg_replace(&#39;/<img[^>]+>/&#39;, &#39;&#39;, $content[1]); 
        echo &#39;找到一条说说:&#39;.$content.&#39; 机器人的回复是:&#39;; 
        $data = &#39;B_&#39;.$data[1]; 
        $content = talk($content); 
        echo $content.&#39;<br />&#39;; 
        $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(&#39; &#39;, &#39;&#39;, $content); 
    $ch = curl_init(); 
  curl_setopt($ch,CURLOPT_URL,&#39;http://www.simsimi.com/talk.htm&#39;); 
  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(&#39;/Set-Cookie: (.+)=(.+)$/m&#39;, $rs, $regs); 
  foreach($regs[1] as $i=>$k); 
  $cc=str_replace(&#39; Path&#39;,&#39;&#39; ,$k); 
  $cc=&#39;simsimi_uid=507454034223;&#39;.$cc; 
  $re = HTTPClient(&#39;http://www.simsimi.com/func/reqN?lc=ch&ft=1.0&req=&#39;.$content.&#39;&fl=http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm&#39;,$cc); 
  $re = json_decode($re,true); 
  return $re[&#39;sentence_resp&#39;]; 
} 
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分頁原理分頁程式碼分頁類別製作方法實例詳解

PHP定時任務取得微信access_token的方法實例分享

#PHP實作取得使用者客戶端真實IP的解決方法

#

以上是PHP基於curl呼叫自動回覆接口的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn