> 代码记录 > PHPCMS V9百度主动推送php代码

PHPCMS V9百度主动推送php代码

<?php

$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = 'root'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
$domain = '1004619.com';   //不带www的域名
$token = 'xxxxxxxxxxx';  //站长token
$http = "http";  //协议请求 http  https
$database = '1004619_com'; //数据库名

$tableArr = [  // 可传入多个表
    'v9_news',
    'v9_keyword',
];

if (empty($tableArr)) {
    die('请填写表名');
}

if (!$conn) {
    die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn, "set names utf8");

//读取文件内容
$file_path = 'artIds.json';

if (file_exists($file_path)) {
    $fp = fopen($file_path, "r");
    $str = fread($fp, filesize($file_path)); //指定读取大小,这里把整个文件内容读取出来
    $jsonStr = str_replace("\r\n", "<br />", $str);

    $jsonArr = json_decode($jsonStr, true);
    foreach ($tableArr as $value) {
        if (!array_key_exists($value, $jsonArr)) {
            $jsonArr[$value] = 0;
        }
    }

    fclose($fp);
} else {
    $myfile = fopen($file_path, "w");

    $jsonArr = [];
    foreach ($tableArr as $value) {
        $jsonArr[$value] = 0;
    }
    fwrite($myfile, json_encode($jsonArr));
    fclose($myfile);
}

$urls = [];
$lastTableName = '';
$lastTableId = 0; 

foreach ($jsonArr as $tableName => $tableArtId) {
    
    if ($tableName == 'v9_news') {
         $sql = "SELECT a.id, a.url FROM v9_news a where a.id>{$tableArtId}  order by a.id limit 1";
        mysqli_select_db($conn, $database);
        //查找资讯--------------------------------------------------
        $retval = mysqli_query($conn, $sql);
        if (!$retval) {
            die('无法读取数据: ' . mysqli_error($conn));
        }

        while ($row = mysqli_fetch_assoc($retval)) {
            $jsonArr[$tableName] = $row['id'];
	        $lastTableName = $tableName;
	        $lastTableId = $row['id'];

            array_push($urls, $http . "://www." . $domain . $row['url']);
        }
    }
   

   if ($tableName == 'v9_keyword') {
        $sql = "SELECT a.id, a.keyword FROM v9_keyword a where a.id>{$tableArtId}  order by a.id limit 0";
        mysqli_select_db($conn, $database);
        //查找资讯--------------------------------------------------
        $retval = mysqli_query($conn, $sql);
        if (!$retval) {
            die('无法读取数据: ' . mysqli_error($conn));
        }

        while ($row = mysqli_fetch_assoc($retval)) {
            $jsonArr[$tableName] = $row['id'];
            $lastTableName = $tableName;
            $lastTableId = $row['id'];

            array_push($urls, $http . "://www." . $domain . "/title/".$row['keyword'].".html");
        }
   }
    
}
//echo ("<pre>");
//var_dump($urls);die;


//$linkApi = 'http://data.zz.baidu.com/urls?site=www.' . $domain . '&token=' . $token;
$mipApi = "http://data.zz.baidu.com/urls?site=www." . $domain . "&token=" . $token . "&type=mip";
$linkCh = curl_init();
$linkOptions = array(
    CURLOPT_URL => $linkApi,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => implode("\n", $urls) ,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: text/plain'
    ) ,
);
curl_setopt_array($linkCh, $linkOptions);
$linkPushResult = curl_exec($linkCh);
$mipCh = curl_init();
$mipOptions = array(
    CURLOPT_URL => $mipApi,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => implode("\n", $urls) ,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: text/plain'
    ) ,
);
curl_setopt_array($mipCh, $mipOptions);
$mipPushResult = curl_exec($mipCh);
// 写入maxId
$linkPushResultObj = json_decode($linkPushResult);
if ($linkPushResultObj->error == 400) {
    exit("推送异常:" . $linkPushResult);
}

$myfile = fopen($file_path, "w") or die("Unable to open file!");
fwrite($myfile, json_encode($jsonArr));
fclose($myfile);
echo ($linkPushResult . "<br>");
echo ($mipPushResult . "<br>");
echo ("最后推送表:" . $lastTableName . "Id:" . $lastTableId);

 

支付宝赞助
微信赞助