Baidu real-time push api application example, api example

Source: Internet
Author: User

Baidu real-time push api application example, api example

For websites with good website quality, you can see the real-time push function under the Baidu webmaster platform/data submission/sitemap column. Currently, this tool is open to users, the api of Baidu's real-time push can push our new articles in real time to ensure that Baidu is included immediately.
 
Baidu webmaster platform http://zhanzhang.baidu.com/


Open the Baidu webmaster platform and click the add data interface of real-time push to get the api push address with token:
 
Http://ping.baidu.com/sitemap? Site = www.yourdomain.com & resource_name = sitemap & access_token = xxxxxxx
 

Share a piece of php real-time push code written by a netizen:

Php pushes new articles in real time

Push sitemap in socketopen Mode

Copy codeThe Code is as follows:
Function sitemap_ping_baidu ($ urls ){
$ Baidu_ping_url = 'Ping .baidu.com ';
$ Get = '/sitemap? Site = www.yourdomain.com & resource_name = sitemap & access_token = xxxxxxx ';
$ Port = 80;
If ($ io = fsockopen ($ baidu_ping_url, $ port, $ errno, $ errstr, 50 ))! = False ){
$ Send = "POST $ get HTTP/1.1". "rn ";
$ Send. = 'Accept: */* '. "rn ";
$ Send. = 'cache-Control: no-cache'. "rn ";

$ Send. = 'host: '. $ baidu_ping_url. "rn ";
$ Send. = 'pragma: no-cache'. "rn ";
// $ Send. = "Referer: http: //". $ url. $ get. "rn ";
// $ Send. = 'user-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0) '. "rn ";

$ Xml = '<? Xml version = "1.0" encoding = "UTF-8"?> <Urlset> ';
Foreach ($ urls as $ url ){
$ Xml. = '<url> ';
$ Xml. = '<loc> <! [CDATA ['. $ url.']> </loc> ';
$ Xml. = '<lastmod>'. date ('Y-m-d'). '</lastmod> ';
$ Xml. = '<changefreq> monthly </changefreq> ';
$ Xml. = '<priority> 0.8 </priority> ';
$ Xml. = '</url> ';
}
$ Xml. = '</urlset> ';

$ Send. = 'content-Length: '. strlen ($ xml). "rn ";
$ Send. = "Connection: Closernrn ";

$ Send. = $ xml. "rn ";

Fputs ($ io, $ send );

$ Return = '';
While (! Feof ($ io ))
{
$ Return. = fread ($ io, 4096 );
}
Return $ return;
} Else {
Return false;
}
}
$ Return = sitemap_ping_baidu (array ('HTTP: // www.yourdomain.com/a.php? Id = 1 '));

Xml document returned by Baidu after push

Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<MethodResponse>
<Params>
<Param>
<Value>
<Int> 200 </int>
</Value>
<Param>
</Params>
</MethodResponse>

The status code is as follows:

200 No usage error. You need to check whether the returned content is correct.
400 Required Parameter not provided
405 Unsupported request method. We only support data submission in POST mode.
411 The Content-Length field is missing in the HTTP Header
413 The pushed data is too large and exceeds the limit of 10 MB.
422 The Length of the Content-Length statement in the HTTP header is inconsistent with the actual Length of the sent data.
500 Internal Server Error of webmaster Platform


How to Use API interfaces?

Your problem is too crude. Different API interfaces provide different calling methods. Generally, the API provider provides the corresponding descriptions and examples.

How to use Baidu sitemapPost push example

With the developer account, you can access the Baidu open cloud platform. The following url is the Development Guide: developer.baidu.com/..qq. C2C. In principle, the sample program provided by Baidu can be successfully run according to this Guide, but I personally feel that it is not detailed enough. I will write down the following steps based on my own experience.
After entering the above website, click Login in the upper-right corner. The Management Console is displayed in the upper-right corner. Click Application Management in the management console, and click the arrow to display the 1.1 mobile application management category. Click mobile application management to go to application management.
Click to enter the following interface, enter the application name, such as push test 2, and click Save.

Click Application Management in the management console, and click the arrow to display the 1.1 mobile application management category. Click developer service management to go to figure 1.4. The push test 2 is just created.

Figure 1.4 developer Service Management
Click push test 2 to go to Figure 1.5 and record the Red Circle ID, which will be written to the program later.

Figure 1.5 Basic Information
Package = "com. baidu. push. example"

Figure 1.6 push settings
If you do not know the application package name when using it for the first time, you should first introduce the SDK pushed by Baidu cloud. Download the android client SDK package and application Demo: developer.baidu.com/...entsdk. The following decompress the package directory as follows \ Baidu-Push-SDK-Android-L2-3.2.0 \ Demo, import the Demo to eclipse. If the editing page appears
The report will be converted into UTF-8 encoding, General will not report the error. In this way, the sample program runs on the mobile phone, and you will find that the program cannot receive the push from Baidu cloud. The reason is that the value of api_key in mainfest. xml is the basic information API key in push test 2. See the red circle in Figure 1.5. My modifications are as follows:
If you run it again, you can receive messages pushed by Baidu cloud. The next section describes the detailed test process.
Run pushdemo on your mobile phone, go to Baidu cloud developer service management, and select push test 2 in the project name. Click push 1.7 on the left;
Click "notification" to enter 1.8, enter the relevant content in the title and message content, and the most critical time is reached. It is best to run pushdemo to the background or exit, in this way, you can feel the push function more vividly. After you click send in the figure, you will hear the sound in about 1 second, indicating that you have received the push and 1.9 test results, this message does not correspond to the content in Figure 1.8, because the result is my next day. I will not talk about the test of messages and media. I have tested it.

Related Article

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.