Baidu real-time push api interface application example, api example _ PHP Tutorial

Source: Internet
Author: User
Baidu real-time push api application example, api example. Baidu real-time push api interface application example. for a website with good api quality, you can view the real-time push function in the sitemap section of Baidu webmaster platform data submission, currently, this tool uses Baidu's real-time push api application 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

The 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"?> ';
Foreach ($ urls as $ url ){
$ Xml. =' ';
$ Xml. =' '.$url.' ';
$ Xml. =' '. Date ('Y-m-D ').' ';
$ Xml. =' Monthly ';
$ Xml. =' 0.8 ';
$ Xml. =' ';
}
$ Xml. =' ';

$ 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

The code is as follows:


<? Xml version = "1.0" encoding = "UTF-8"?>




200




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. 4. the push test 2 is just created.

. 4 developer service management
Click push test 2 to enter. 5, record the Red Circle ID, and then write it into the program.

. 5 Basic information
Package = "com. baidu. push. example"

. 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 you need to modify the value of api_key in mainfest. xml, which is the basic information of API key in push test 2. see the red circle in. 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 and. the content in 8 does not match, because the result is my next day. I will not talk about the test of messages and media. I have tested it.

Websites with good quality can see the real-time push function under the Baidu webmaster platform/data submission/sitemap column. Currently, this tool...

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.