Soms wil je bij SEO een programma gebruiken om de titel automatisch aan te passen. Op dit moment kunt u het Baidu-vervolgkeuzewoordenboek gebruiken om de wijziging automatisch te matchen. Het volgende is een PHP-code om het Baidu-woordenboek te verkrijgen.
//获取百度下拉信息
$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;//数据随机,防止与第一个百度标题一样Geoptimaliseerde 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)];
}Codeverbeteringen omvatten:
- Gebruik een array om willekeurige headeropties op te slaan om duplicatie te voorkomen
- Gebruik
array_columnKopteksten uit het antwoord extraheren - Gebruik
array_randHaal een willekeurige titel op uit de optiematrix - Vereenvoudigd om duplicatie van code met originele titel en eerdere suggesties te voorkomen
- Stel curl-opties in met behulp van array-argumenten om de leesbaarheid en onderhoudbaarheid te verbeteren
Volgens de bovenstaande code is het ingekapseld in een functie. Als na het doorgeven van de Chinese titel de vervolgkeuzelijst met succes is verkregen, wordt de vervolgkeuzelijst geretourneerd. Als de acquisitie leeg is, wordt false geretourneerd.
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;
}
}Wijzig de code volgens uw behoeften:
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;
}
}