Baidu real-time push api interface application example, api example_PHP tutorial

WBOY
Release: 2016-07-13 10:16:26
Original
2505 people have browsed it

Baidu real-time push api interface application example, api example

Websites with good website quality can see the real-time push function under the Baidu Webmaster Platform/Data Submission/sitemap column. This tool is currently open to invitations. Baidu’s real-time push API interface can push our newly published articles in real time. , ensuring that Baidu will include it as soon as possible.

Baidu Webmaster Platform http://zhanzhang.baidu.com/


Open Baidu Webmaster Platform, click on the add new 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 newly published articles in real time

Socketopen method to push sitemap

Copy code 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 = '';
              foreach($urls as $url){
$xml .= '';
$xml .= '';
                      $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'));


The xml document that Baidu will return after pushing

Copy code The code is as follows:



                                                                                                                                               
                                                                                                                                                                                                                                                                                                                             ​                                                                                                                                                                               
                                                                                                  





The meaning of the status code is as follows

How to use the API interface?

Your question is too crude. Different API interfaces provide different calling and usage methods. Under normal circumstances, the API provider will provide corresponding instructions and examples.

How to use Baidu sitemapPost push example

After you have a developer account, enter the Baidu Open Cloud Platform. The following URL is the development guide: developer.baidu.com/...qq.c2c. In principle, you can successfully run the sample program provided by Baidu according to this guide, but I personally feel that it is not detailed enough, so I wrote the following steps based on my own experience.
After entering the website as shown above, click Login in the upper right corner. After success, the management console will be displayed in the upper right corner. Click the management console to enter the light application management, and then click the arrow in the picture to display the mobile application management classification as shown in Figure 1.1. Click the mobile application management to enter the application management.
Click to enter the following interface, fill in the application name, such as Push Test 2, and click Save.

Click on the management console to enter the light application management, and then click on the arrow in the picture to display the mobile application management classification in Figure 1.1. Click on the developer service management to enter Figure 1.4. The push test 2 is the newly created one. .

Figure 1.4 Developer Service Management
Click Push Test 2 to enter the red circle ID in Figure 1.5 and record the ID in the red circle, which will be written into the program later.

Figure 1.5 Basic information
package="com.baidu.push.example"

Figure 1.6 Push settings
You may not know the application package when you use it for the first time. What is the name? Let’s first introduce the SDK of Baidu Cloud Push. Download the android version of the client SDK package and application demo from this website: developer.baidu.com/...entsdk. Next, unzip the package directory as follows\Baidu-Push-SDK-Android-L2-3.2.0\Demo, and import the Demo into eclipse. If there is an editor
translation report, just change the encoding to UTF-8, and generally there will be no error. In this way, if you run the sample program on your mobile phone, you will find that the program cannot receive push notifications from Baidu Cloud. This is because the value of api_key in mainfest.xml needs to be modified, which is the basic information API key in test 2 just pushed. See the red circle in Figure 1.5. My modifications are as follows:
Run it again to receive messages pushed by Baidu Cloud. The next section introduces the detailed testing process.
Run the pushdemo software on your mobile phone, enter Baidu Cloud Developer Service Management, and select the push test 2 just now in the project name. Click on the cloud push on the left as shown in Figure 1.7;
Click on the notification to enter the following Figure 1.8. Fill in the relevant content in the title and message content. The last and most critical time has arrived. It is best to run the pushdemo to the background or exit, so Only then can you feel the push function more vividly. Click Send in the picture. In about 1 second, you will hear a sound, indicating that you have received the push. As shown in Figure 1.9, the test result. This message is the same as in Figure 1.8. The content does not correspond because the results are mine the next day. I won’t talk about the testing of messages and media. I’ve tested it and it should be OK.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897688.htmlTechArticleBaidu pushes api interface application examples in real time. Websites with good api example website quality can be submitted on Baidu webmaster platform/data Under the /sitemap column, you can see the real-time push function. Currently, this tool...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template