Sometimes when doing SEO, you want to use a program to automatically modify the title. At this time, you can use the Baidu drop-down dictionary to automatically match the modification. The following is a PHP code to obtain the Baidu dictionary.
//获取百度下拉信息
$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;//数据随机,防止与第一个百度标题一样Optimized code:
<?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)];
}Code improvements include:
- Use an array to store random header options to avoid duplication
- Use
array_columnExtract headers from response - Use
array_randGet random title from options array - Simplified to avoid duplication of code with original title and previous suggestions
- Set curl options using array arguments to improve readability and maintainability
According to the above code, it is encapsulated into a function. After passing in the Chinese title, if the drop-down is successfully obtained, the drop-down array will be returned. If the acquisition is empty, false will be returned.
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;
}
}Change the code according to your needs:
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;
}
}