This article mainly introduces the use example of Baidu ping method. you can automatically ping the website address to Baidu. for details, refer to how to use this method to automatically ping Baidu, my method is to generate the cache. when the cache is updated, Baidu is automatically pinged. if the returned result is 0, the cache is successfully updated.
The code is as follows:
$ Pingarcurl = 'http: // '. $ _ SERVER ['http _ host']. $ _ SERVER ['request _ URI'];
$ Arcping = new Ping ('', $ pingarcurl, get_domain (), 'http: // www.bitsCN.com ');
$ Arcping-> pingbaidu ();
The code is as follows:
Class ping {
Private $ title; // blog name
Private $ hosturl; // The homepage address of the blog.
Private $ arturl; // New article address
Private $ rssurl; // Blog rss address
Private $ baiduXML; // Baidu XML structure
Private $ baiduRPC; // Baidu XML address
Publicfunction _ construct ($ title, $ arturl, $ hosturl, $ rssurl)
{
If (empty ($ title) | empty ($ arturl ))
Returnfalse;
$ This-> title = $ title;
$ This-> hosturl = $ hosturl;
$ This-> rssurl = $ rssurl;
$ This-> arturl = $ arturl;
$ This-> baiduRPC = 'http: // ping.baidu.com/ping/rpc2 ';
$ This-> baiduXML =' ';
$ This-> baiduXML. =' ';
$ This-> baiduXML. =' WeblogUpdates. extendedPing ';
$ This-> baiduXML. =' ';
$ This-> baiduXML. =' '. $ This-> hosturl .' ';
$ This-> baiduXML. =' '. $ This-> title .' ';
$ This-> baiduXML. =' '. $ This-> arturl .' ';
$ This-> baiduXML. =' '. $ This-> rssurl .' ';
$ This-> baiduXML. =' ';
$ This-> baiduXML. =' ';
}
Publicfunctionpingbaidu ()
{
$ Ch = curl_init ();
$ Headers = array (
'User-Agent: request ',
'Host: ping.baidu.com ',
'Content-Type: text/XML ',
);
Curl_setopt ($ ch, CURLOPT_URL, $ this-> baiduRPC );
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ this-> baiduXML );
$ Res = curl_exec ($ ch );
Curl_close ($ ch );
// Return $ res;
Return (strpos ($ res ," 0 "))? True: false;
}
}
Functionget_domain ()
{
/* Protocol */
$ Protocol = 'http ://';
/* Domain name or IP address */
If (isset ($ _ SERVER ['http _ X_FORWARDED_HOST ']) {
$ Host = $ _ SERVER ['http _ X_FORWARDED_HOST '];
} Elseif (isset ($ _ SERVER ['http _ host']) {
$ Host = $ _ SERVER ['http _ host'];
} Else {
/* Port */
If (isset ($ _ SERVER ['server _ port']) {
$ Port = ':'. $ _ SERVER ['server _ port'];
If (': 80' = $ port & 'http: //' = $ protocol) | (': 000000' = $ port &' https: // '= $ protocol )){
$ Port = '';
}
} Else {
$ Port = '';
}
If (isset ($ _ SERVER ['server _ name']) {
$ Host = $ _ SERVER ['server _ name']. $ port;
} Elseif (isset ($ _ SERVER ['server _ ADDR ']) {
$ Host = $ _ SERVER ['server _ ADDR ']. $ port;
}
}
Return $ protocol. $ host;
}
// $ Arc = newPing ('website title', 'updated URL address', 'website domain name', 'RSS address ');
// $ Arc = newPing ('your website title', get_arcurl ($ id), get_domain (), 'http: // www.abc.com/rss.php ');
// Echo $ arc-> pingbaidu ();
// If the returned result is 0, the operation is successful.