日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php定时发布微博,使用sae定时任务实现终身自动发表微博(PHP实现)

發布時間:2023/12/10 php 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php定时发布微博,使用sae定时任务实现终身自动发表微博(PHP实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用sae定時任務實現終身自動發表微博(PHP實現)

實例環境:

新浪微博sae平臺申請的二級開發域名,使用thinkphp框架。

關于thinkphp到sae的代碼移植這里不討論。

關于sae的授權認證,這里不討論。

關于sae的mysql,kvdb如何使用,這里不討論。

以上入門知識請參看:

http://doc.thinkphp.cn/manual/sae_intro.html

原理:

任務1:講時間線的微博抓取,存到緩存;

任務2:從緩存取出第一條,發表,從緩存刪除。

本應用預備的數據庫:

sae_wid:

字段 類型 說明

wid ? long long 授權帳號的uid

token var char 授權帳號的access_token

其他字段略(expired_in,auth_time,screen_name,...)

在ThinkPHP中直接加Action,不用加Tpl。

這里直接上代碼:

/* GraspAction.php */

include_once( APP_PATH.'/Conf/config_svn.php' );

import('@.ORG.saetv2');

/**

* 隨機抓取微博

*/

class GraspAction extends Action {

protected function _initialize() {

header("Content-Type:text/html; charset=utf-8");

}

public $myacct = '14********';//微博

public $listen_list = array(

'微博昵稱1',

'微博名稱2',

'**笑話**',

'我的前任**',

'搞笑熱咨詢',

'趣聞搞笑',

'搞笑圖片會',

);

// 從時間線抓取微博

public function mining()

{

$uid = $this->myacct;

// 從數據庫查找 myacct 的 token

// 獲取 uid 的 accessToken

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 將時間線的有名微博抓出來

$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);

$ms = $c->home_timeline();

if (isset($ms['error']))

{

$this->error($ms['error']);

}

dump(count($ms['statuses']));

$line = $ms['statuses'];

$houxuan = array();

for ($i=0; $i

{

if (in_array($line[$i]['user']['name'], $this->listen_list))

{

$houxuan[] = $line[$i];

}

}

// 存入緩存

F('weibohouxuan', json_encode($houxuan));

print_r('F(weibohouxuan)='.json_encode(F('weibohouxuan')));

}

// 將緩存的候選內容發表到微博,并刪除

public function put()

{

// 獲取 token

$uid = $this->myacct;

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 獲取緩存內容列表

$houxuan = json_decode(F('weibohouxuan'), true);

// 取出一條 并從緩存列表刪除

$one = array_shift($houxuan);

//dump($one);return false;

if (empty($one))

{

$this->error('No candicate weibo content!');

}

$text = $one['text'];

//dump($text);return false;

$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);

// 如果有圖片,使用圖片發布接口

if (isset($one['original_pic']) && !empty($one['original_pic']))

{

//$ret = $c->upload_url_text($text, $one['original_pic']);

$ret = $c->upload($text, $one['original_pic']);

print_r('put pic :'.json_encode($ret));

}

else // 無圖片 使用無圖片接口

{

$ret = $c->update($text);

print_r('put :'.json_encode($ret));

}

F('weibohouxuan', json_encode($houxuan));

}

// 將緩存的候選內容顯示

public function show()

{

// 獲取 token

$uid = $this->myacct;

$mysql = new SaeMysql();

try {

$u = $mysql->getData('SELECT wid,token FROM sae_wid WHERE wid='.$uid);

} catch (Exception $e) {

$this->error($e->getMessage());

}

if (empty($u))

{

$this->error('cannot get data from Sae Mysql');

}

$token = $u[0]['token'];

// 獲取緩存內容列表

$houxuan = json_decode(F('weibohouxuan'), true);

dump($houxuan);

return true;

}

}

/* GraspAction.php over */

下面是sae的cron設置,對應文件 config.yaml

name: dajiayiqier

version: 2

handle:

- directoryindex: ?index.php

accesskey: dajiayiqier.sinaapp.com

cron:

- description: another cron put

url: w/index.php/Grasp/put

schedule: */55 * * * *

- description: another cron get

url: w/index.php/Grasp/minnine

schedule: 15 */6 * * *

關于crontab格式,不介紹,貼吧中不允許發太多連接,自己查。

祝大家微博玩得愉快!

總結

以上是生活随笔為你收集整理的php定时发布微博,使用sae定时任务实现终身自动发表微博(PHP实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。