在博客园看到一篇这样的帖子,转载过来,还没测试,目测没有问题,有需要的测试一下吧。
<?php class zhanzhangPush { private $dbhost = 'localhost:3306'; // mysql服务器主机地址 private $dbuser = ''; // mysql用户名 private $dbpass = ''; // mysql用户名密码 private $domain = ''; //不带www的域名 private $zhanzhangToken = ''; //站长token private $xiongToken = ''; //熊掌号token private $xiongAppid = ''; //熊掌号appid private $shenmaUserName = ''; //神马username private $shenmaToken = ''; //神马token private $http = "https"; //协议请求 http https private $database = ''; //数据库名 private $www = 'm'; //域名前缀 www m public function init() { $this->shenma_push($urls); // $this->baidu_push($urls); // $this->real_push($urls); // $this->batch_push($urls); $this->sogouPush($urls); } /** * 获取网站url */ public function get_site_urls($artId, $vodId, $limit = 800) { $limit = $limit / 2; $conn = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass); if (!$conn) { die('连接失败: ' . mysqli_error($conn)); } //var_dump($conn);die; mysqli_query($conn, "set names utf8"); mysqli_select_db($conn, $this->database); //var_dump($artId);die; //var_dump($urls);die; //查找电影-------------------------------------------------- $sql = "SELECT titleurl,id FROM www_96kaifa_com_ecms_news where id > {$artId} limit $limit"; $retval = mysqli_query($conn, $sql); if (!$retval) { die('无法读取数据: ' . mysqli_error($conn)); } $urls = []; while ($row = mysqli_fetch_assoc($retval)) { $artId = $row['id']; // echo 'vod'.$vodId.'<br>'; array_push($urls, $this->http . "://m." . $this->domain . $row['titleurl']); } $data = ['urls' => $urls, 'art_id' => $artId, 'vod_id' => $vodId]; return $data; } /** * 获取api接口 */ public function get_api($zhanzhang) { switch ($zhanzhang) { case 'shenma': return "http://data.zhanzhang.sm.cn/push?site=".$this->www."." . $this->domain . "&user_name=" . $this->shenmaUserName . "&resource_name=mip_add&token=" . $this->shenmaToken; case 'realtime': return "http://data.zz.baidu.com/urls?appid=" . $this->xiongAppid . "&token=" . $this->xiongToken . "&type=realtime"; case 'batch': return "http://data.zz.baidu.com/urls?appid=" . $this->xiongAppid . "&token=" . $this->xiongToken . "&type=batch"; case 'baidu': return "http://data.zz.baidu.com/urls?site=".$this->www."." . $this->domain . "&token=" . $this->zhanzhangToken; } } /** * 推送 */ public function push($api, $urls) { // var_dump($urls);die; $ch = curl_init(); $options = array(CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, // CURLOPT_POSTFIELDS => implode("\n", array_slice($urls,0,10)), CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),); curl_setopt_array($ch, $options); $res = curl_exec($ch); return $res; } /** * 神马站长推送 */ public function shenma_push($urls) { $configData = $this->get_config(); $data = $this->get_site_urls($configData['shenma']['art_id'], $configData['shenma']['vod_id']); $urls = $data['urls']; $configData['shenma']['art_id'] = $data['art_id']; $configData['shenma']['vod_id'] = $data['vod_id']; //推送接口 $api = $this->get_api('shenma'); $res = $this->push($api, $urls); $jsonres = json_decode($res); if ($jsonres->errorMsg == '') { $this->log("神马站长成功推送" . count($urls) . "条"); $this->update_config($configData); } else { $this->log("神马站长推送失败:" . $res); } // echo "<pre>";var_dump($configData);die; var_dump($res); } /** * 百度站长推送 */ public function baidu_push($urls) { $configData = $this->get_config(); $data = $this->get_site_urls($configData['baidu']['art_id'], $configData['baidu']['vod_id']); $urls = $data['urls']; $configData['baidu']['art_id'] = $data['art_id']; $configData['baidu']['vod_id'] = $data['vod_id']; $api = $this->get_api('baidu'); $res = $this->push($api, $urls); $jsonres = json_decode($res); if ($jsonres->error >= 400) { $this->log("百度站长推送失败:" . $res); } else { $this->log("百度站长成功推送" . $jsonres->success . "条,今日还可推送:" . $jsonres->remain . "条"); $this->update_config($configData); } var_dump($urls);die; echo "<pre> 百度站长推送"; var_dump($res); } /** * 熊掌号天级推送 */ public function real_push($urls) { $configData = $this->get_config(); $data = $this->get_site_urls($configData['realtime']['art_id'], $configData['realtime']['vod_id'], 10); $urls = $data['urls']; $configData['realtime']['art_id'] = $data['art_id']; $configData['realtime']['vod_id'] = $data['vod_id']; $api = $this->get_api('realtime'); $res = $this->push($api, $urls); $jsonres = json_decode($res); if ($jsonres->error >= 400) { $this->log("熊掌号天级推送失败:" . $res); } else { $this->log("熊掌号天级成功推送" . $jsonres->success . "条,今日还可推送:" . $jsonres->remain . "条"); $this->update_config($configData); } // var_dump($urls);die; echo "<pre>"; var_dump($res); } /** * 熊掌号周级推送 */ public function batch_push($urls) { $configData = $this->get_config(); $data = $this->get_site_urls($configData['batch']['art_id'], $configData['batch']['vod_id']); $urls = $data['urls']; $configData['batch']['art_id'] = $data['art_id']; $configData['batch']['vod_id'] = $data['vod_id']; $api = $this->get_api('batch'); $res = $this->push($api, array_slice($urls, 0, 1)); $jsonres = json_decode($res); if ($jsonres->error >= 400) { $this->log("熊掌号周级推送失败:" . $res); } else { $this->log("熊掌号周级成功推送" . $jsonres->success . "条,今日还可推送:" . $jsonres->remain . "条"); $this->update_config($configData); } // var_dump($urls);die; echo "<pre>"; var_dump($res); } /** *搜狗推送 */ public function sogouPush($urls) { $configData = $this->get_config(); $data = $this->get_site_urls($configData['sougou']['art_id'], $configData['sougou']['vod_id'], 10); $urls = $data['urls']; $configData['sougou']['art_id'] = $data['art_id']; $configData['sougou']['vod_id'] = $data['vod_id']; echo ("<pre>"); // var_dump($data);die; for ($i = 0;$i < count($urls);$i++) { $postUrl = 'http://fankui.help.sogou.com/index.php/web/web/addShoulu'; $postData = array("source" => "1", "site_type" => "1", "Shoulu" => array("webAdr" => $urls[$i], "email" => "103@qq.com", "reason" => "")); $postData = http_build_query($postData); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); curl_setopt($curl, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $res = curl_exec($curl); curl_close($curl); $jsonres = json_decode($res); if ($jsonres->success == 'true') { $this->log("搜狗推送成功:" . $urls[$i]); } else { $this->log("搜狗推送失败:" . $urls[$i]); } } $this->update_config($configData); } /** * 获取配置信息 */ public function get_config() { $configPath = 'push_config.txt'; if (file_exists($configPath)) { $data = file_get_contents($configPath); return unserialize($data); } else { $myfile = fopen($configPath, "w"); $configData = [ 'shenma' => [ 'art_id' => 0, 'vod_id' => 0 ], 'baidu' => [ 'art_id' => 0, 'vod_id' => 0 ], 'realtime' => [ 'art_id' => 0, 'vod_id' => 0 ], 'batch' => [ 'art_id' => 0, 'vod_id' => 0 ], 'sougou' => [ 'art_id' => 0, 'vod_id' => 0 ], ]; fwrite($myfile, serialize($configData)); fclose($myfile); return $configData; } } /** * 修改配置信息 */ public function update_config($configData) { $configPath = 'push_config.txt'; $myfile = fopen($configPath, "w"); fwrite($myfile, serialize($configData)); fclose($myfile); } public function log($data) { file_put_contents('zhanzhang_push.log', date('Y-m-d H:i:s') . " " . $data . PHP_EOL, FILE_APPEND); } } $obj = new zhanzhangPush(); $obj->init();
原创文章,作者:Tony,如若转载,请注明出处:https://www.xxside.com/4688.html
思德心语,壹群:799239814