Baidu ping method example

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.