The Baidu ping method uses the example to automatically ping Baidu. When using this method to access Baidu, I can use this method to generate a cache. when updating the cache, I can automatically ping Baidu. if the returned result is 0, the request is successful. Copy the code as follows: $ p is automatically pinged to Baidu during access using the method. my method is to generate the cache and automatically ping Baidu when updating the cache. if the returned result is 0, the request is successful.
The code is as follows:
$ Pingarcurl = 'http: // '. $ _ SERVER ['http _ host']. $ _ SERVER ['request _ URI'];
$ Arcping = new Ping ('feet home', $ pingarcurl, get_domain (), 'http: // www.jb51.net ');
$ 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.
Bytes. The code is as follows: $ p...