Home >php教程 >PHP源码 >段子搬运工

段子搬运工

PHP中文网
PHP中文网Original
2016-05-26 08:19:041702browse

作为开源中国首席段子手,必然有发布段子的特殊技巧

段子搬运工

从煎蛋获取最新的段子后,登录OSC并发表动弹。并不需要数据库的支持。

配置项

Joke.txt以及cookie.txt文件需要777权限

需要知道您在OSC的加密后的密码和user_id和user_code

再配合 crontab */5 * * * * /usr/bin/curl http://jokeporter.fans7.me/JokePorter.php

每5分钟检测一次是否有新段子发布

Github:https://github.com/wangkugou1983/JokePorter

下一个计划 “开源中国点赞狂魔”

<?php
//段子搬运工
class JokePorter{
    public function index(){
        $this->get_joke(); //获得段子
    }
    //获得段子
    private function get_joke(){
        $html = file_get_contents(&#39;http://jandan.net/duan&#39;,NULL,NULL,-1,10000);
        preg_match_all(&#39;/<p>[\s\S]*?<\/p>/&#39;,$html,$res);
        $joke = strip_tags($res[0][2]); //得到段子
        $file = file_get_contents(&#39;./Joke.txt&#39;); //本地段子
        $joke != $file ? $this->stash_joke($joke) : &#39;&#39;; //如果段子比本地新,储存段子在本地
    }
    //储存段子
    private function stash_joke($joke){
        $handle = fopen(&#39;./Joke.txt&#39;,"w");
        fwrite($handle,$joke);
        fclose($handle);
        $this->login_osc(); //登录OSC
    }
    //登录OSC
    private function login_osc(){
        //登录
        $post = array(&#39;email&#39; => &#39;你的OSC邮箱账号&#39;,&#39;pwd&#39; => &#39;加密后的密码&#39;,&#39;save_login&#39; => &#39;1&#39;);
        $curl = curl_init(); //初始化curl模块
        curl_setopt($curl,CURLOPT_URL,&#39;https://www.oschina.net/action/user/hash_login&#39;); //登录提交的地址
        curl_setopt($curl,CURLOPT_HEADER,0); //是否显示头信息
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,0);
        curl_setopt($curl,CURLOPT_COOKIEJAR,&#39;./cookie.txt&#39;); //设置cookie信息保存在指定的文件中
        curl_setopt($curl,CURLOPT_POST,1); //post方式提交
        curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($post)); //要提交的信息
        $res = curl_exec($curl); //执行cURL
        $res ? $this->push_joke($curl) : curl_close($curl);
    }
    //发布段子
    private function push_joke($curl){
        $file = file_get_contents(&#39;./Joke.txt&#39;); //本地段子
        $send = array(&#39;msg&#39; => $file,&#39;user&#39; => &#39;你的user_id&#39;,&#39;user_code&#39; => &#39;你的user_code&#39;);
        curl_setopt($curl,CURLOPT_URL,&#39;https://www.oschina.net/action/tweet/pub&#39;);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //curl_exec将结果返回,而不是执行
        curl_setopt($curl,CURLOPT_POST,1); //post方式提交
        curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($send)); //要提交的信息
        curl_exec($curl); //执行cURL
        curl_close($curl);
    }
}
$JokePorter = new JokePorter();
$JokePorter->index();

以上就是段子搬运工的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn