php获取百度下拉词

有些时候在做SEO的时候想要使用程序来自动修改标题,这时候可以用到百度下拉词库来自动匹配修改,以下是一段获取百度词库的PHP代码

//获取百度下拉信息
$title = urlencode($data['title']);
$api = "https://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd=".$title."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
$r = json_decode($r,true);

if(empty($r['g'])){
$new_title = $data['title'].'(图文)';
}

//数据一定存在,取出数据,然后随机获取一个
$baidu_title = $r['g'][mt_rand(0,count($r['g'])-1)]['q'];//数据的随机获取
$baidu_title2 = $r['g'][mt_rand(0,count($r['g'])-1)]['q'];//数据的随机获取
$baidu_title = $baidu_title==$data['title']?$r['g'][mt_rand(0,count($r['g'])-1)]['q']:$baidu_title;//数据随机,防止与原标题一样
$baidu_title2 = $baidu_title==$baidu_title2?$r['g'][mt_rand(0,count($r['g'])-1)]['q']:$baidu_title2;//数据随机,防止与第一个百度标题一样

优化后的代码:

<?php
// 获取百度下拉信息
$title = urlencode($data['title']);
$api = "https://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd=".$title."";

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $api,
    CURLOPT_TIMEOUT => 5,
    CURLOPT_RETURNTRANSFER => true,
]);

$r = curl_exec($ch);
curl_close($ch);

$r = json_decode($r, true);

// 检查是否为空,如果为空,则添加后缀到标题中
if (empty($r['g'])) {
    $new_title = $data['title'] . ' (图文)';
}

// 从建议中获取随机标题
$titles = array_column($r['g'], 'q');
$random_title = $titles[array_rand($titles)];

// 避免与原始标题和先前的建议重复
if ($random_title == $data['title'] || $random_title == $baidu_title) {
    $random_title = $titles[array_rand($titles)];
}

$baidu_title = $random_title;
$baidu_title2 = $titles[array_rand($titles)];

if ($baidu_title2 == $baidu_title) {
    $baidu_title2 = $titles[array_rand($titles)];
}

代码的改进包括:

  • 使用数组来存储随机标题选项,以避免重复
  • 使用 array_column 提取响应中的标题
  • 使用 array_rand 从选项数组中获取随机标题
  • 简化了避免与原始标题和先前建议重复的代码
  • 使用数组参数设置 curl 选项,以提高可读性和可维护性

根据上面上面的代码封装成函数,传入中文标题后,如果成功获取下拉则返回下拉数组,获取为空则返回false

function getBaiduSuggestion($title) {
    $title = urlencode($title);
    $api = "https://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd=".$title."";

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $api,
        CURLOPT_TIMEOUT => 5,
        CURLOPT_RETURNTRANSFER => true,
    ]);

    $r = curl_exec($ch);
    curl_close($ch);

    $r = json_decode($r, true);

    if (!empty($r['g'])) {
        $titles = array_column($r['g'], 'q');
        $baidu_title = $titles[array_rand($titles)];

        if ($baidu_title == $title) {
            $baidu_title = $titles[array_rand($titles)];
        }

        $baidu_title2 = $titles[array_rand($titles)];

        if ($baidu_title2 == $baidu_title) {
            $baidu_title2 = $titles[array_rand($titles)];
        }

        return [$baidu_title, $baidu_title2];
    } else {
        return false;
    }
}

根据自己的需求将代码改了改:

function getBaiduSuggestion($title) {
    $title = urlencode($title);
    $api = "https://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd=".$title."";

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $api,
        CURLOPT_TIMEOUT => 5,
        CURLOPT_RETURNTRANSFER => true,
    ]);

    $r = curl_exec($ch);
    curl_close($ch);

    $r = json_decode($r, true);

    if (!empty($r['g'])) {
        return array_column($r['g'], 'q');
    } else {
        return false;
    }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞5
评论 抢沙发

请登录后发表评论

    暂无评论内容