Some websites automatically generate a sitemap.xml file, so we don’t need to manually update the URLs. We just need a function to read the sitemap.xml file and then submit it to Baidu Webmaster with one click.

Code

We use PHP’s built-in simplexml_load_file function to read the sitemap.xml file and use curl to submit these links to Baidu Webmaster.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$site = "Website Address";
$token = "Baidu Webmaster Token";
$xml = simplexml_load_file('sitemap.xml');
$urls = array();
foreach ($xml->url as $url) {
$urls[] = $url->loc;
}
if(count($urls) > 0){
$api = "http://data.zz.baidu.com/urls?site=$site&token=$token";
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
$success = json_decode($result)->success;
if($success){
echo "Push Successful";
}
else{
echo "Push Failed";
}
}

The overall implementation is relatively simple. After reading, it automatically generates an array, which can be directly used in the curl submission.