Note: Only the webmaster who has the sitemap permission can perform this operation. Otherwise, you cannot submit without the access_token password.
1. As shown in the following figure, the following page is displayed.
The following is an official curl recommendation address.
The code is as follows: |
Copy code |
Write xml data into a local file, such as thread. xml, and then call the curl Command: Curl-H 'expect CT: '-- data-binary @ thread. xml "http://ping.baidu.com/sitemap? Site = www.111cn.net & resource_name = sitemap & access_token = your key" You can use php, python, and java to push structured data. |
The returned result is 200. The following example uses php curl to implement post. The code is as follows:
The code is as follows: |
Copy code |
<? Function tobaidu ($ url ){ $ Ntime = time (); $ Now = date ('Y-m-D', $ ntime ); $ Data = '<? Xml version = "1.0" encoding = "UTF-8"?> '; $ Data. = '<urlset> '; $ Data. = '<url> '; $ Data. = '<loc> <! [CDATA ['. $ url.']> </loc> '; $ Data. = '<lastmod>'. $ now. '</lastmod> '; $ Data. = '<changefreq> always </changefreq> '; $ Data. = '<priority> 1.0 </priority> '; $ Data. = '</url> '; $ Data. = '</urlset> '; $ Pingurl = "http://ping.baidu.com/sitemap? Site = www.111cn.net & resource_name = sitemap & access_token = your key value "; // your interface address $ Curl = curl_init (); // Start a CURL session Curl_setopt ($ curl, CURLOPT_URL, $ pingurl); // address to be accessed Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate Curl_setopt ($ curl, CURLOPT_USERAGENT, "curl/7.12.1"); // simulate the browser used by the user Curl_setopt ($ curl, CURLOPT_COOKIE, $ cookie ); Curl_setopt ($ curl, CURLOPT_REFERER ,""); Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops. Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream. $ TmpInfo = curl_exec ($ curl); // perform the operation If (curl_errno ($ curl )){ Echo 'errno'. curl_error ($ curl); // catch an exception } Curl_close ($ curl); // Close the CURL session Return $ tmpInfo; // return data } $ Tt = tobaidu ("http://www.111cn.net/"); // pushes the url of a new article in real time Echo $ tt; ?> |
Return results after submission
The code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "UTF-8"?> <MethodResponse> <Params> <Param> <Value> <Int> 200 </int> </Value> </Param> </Params> </MethodResponse> |
Note that the code is successfully submitted only when the 200 status code is returned. Next let's take a look at the status code.
200 no usage error. Check whether the returned content is correct.
400 required parameter not provided
405 the request method is not supported. We only support the POST method to submit data.
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 webmasters platform server internal error
After using this commit, Xiaobian will return real-time data in sitemap and return data errors and correctness.
Experience Summary
In the previous article, we mentioned the xml document. In fact, we can use post as a string to achieve the same effect. The small editor's approach is to automatically push the article to Baidu after publishing the article.
Note: The pushed article must be an absolute address, such as a http://www.111cn.net instead of/aaa/B .htm if it is a complete address.