class baiduPusher { private $site; private $token; function __construct(){ $configInfo = json_decode(file_get_contents("./config.txt")); $this->site = $configInfo->site; $this->token = $configInfo->token; } /*保存百度站长的配置文件信息 包含需要推送的网址$site 百度站长的token; * */ public function saveConfig(){ $configInfo =array(); $configInfo['site'] = "www.beijing8.cc"; $configInfo['token']="8LUBmE8SKX9dTrhE"; //把信息保存到config.txt; file_put_contents("./config.txt",json_encode($configInfo)); } /* * 自动获取网址并调用推送 * */ public function autoPush(){ $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $urls = array("$url"); $this->pushurl($urls); } /* * 提交网址到百度 * $urls为网址数组 * */ public function pushurl($urls){ $api = 'http://data.zz.baidu.com/urls?site='."$this->site".'&token='."$this->token"; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode(" ", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); return curl_exec($ch); } }