Use http protocol to Post blog post comments. _ PHP Tutorial

Source: Internet
Author: User
Use the http protocol to publish blog post comments ,. POST blog posts and comments using http protocol. this blog POST undertakes "php uses socket to send GET and POST requests" and uses the encapsulated Http class, consider how to use a php script to Post blog post comments to a blog over http,

This blog POST undertakes "php uses socket to send GET and POST requests". we should use the encapsulated Http class above to consider how to submit comments to blog posts in the blog garden through php scripts.

  

  Principle:

Before doing this, we must first understand that the essence of submitting a comment to a blog post is to send a post request through the http server. What do we need to do before publishing a comment? Yes, you must log on. However, login is another thing we will not discuss here. After a user logs on, the server sets a cookie for the client. Http is stateless. That is to say, after the client sends a request to the server, the server returns a response. One communication is complete. The server does not remember who sent the request to itself. Therefore, the client sends a request to the server with the cookie set by the server and notifies the server of its identity. the server generates a response based on the cookie. The principle is so simple. let's take a look at our practice.

Preparations:  

In order to complete this test, I registered a blog post account (DeanHuangChopper). after logging on to the blog page, I opened my blog (DeanChopper) and opened one of my blog posts, for example, the article "understanding the buffer mechanism with php ob functions" (I am using Firefox, the biggest advantage is that the parameters sent to the server can be intuitively seen), open the developer option, prepare to record the process of sending comments. I write a comment and post a comment. This request has been recorded by the developer options. Public function post ($ body) {$ this-> setLine ('post'); // reset the content-type $ this-> setHeader ('content-Type: application/json; charset = UTF-8 '); // skip the setBody method // $ this-> setBody ($ body); $ this-> body [] = $ body; // calculate content-length $ this-> setHeader ('content-length :'. strlen ($ this-> body [0]); $ this-> request (); return $ this-> response ;}

After modifying the Http class again, we can write the main code in this article. Theoretically, you only need to set the cookie value when setting the header information, but it is best to send all the header information to improve the success rate.

Before sending a comment, take a look at the sent parameters:

 Code Section:

The main code of this article is as follows:

 SetHeader ('Accept-Language: zh-CN, zh; q = 0.8, en-US; q = 0.5, en; q = 100 '); $ http-> setHeader ('Accept-Language: zh-CN, zh; q = 0.8, en-US; q = 0.5, en; q = 100 '); $ http-> setHeader ('Accept-Encoding: gzip, deflate'); $ http-> setHeader ('x-Requested-With: XMLHttpRequest '); $ http-> setHeader ('referer: Caller); $ http-> setHeader ('cookie: _ ga = GA1.2.1359064105.1438444082; _ gads = ID = e0c32fd6db6e2a6d: T = 1438443900: S = ALNI_Mb6AAflcBD6gcdHgeE3IqVDJYnnjA ;. CNBlogsCookie = response; _ 5t_trace_sid = response; _ 5t_trace_tms = 1; _ gat = 1'); $ http-> setHeader ('pragma: no-cache '); $ http-> setHeader ('cache-Control: no-cache ');
// Set request body information
$ Msg = '{"blogApp": "DeanChopper", "postId": 4688667, "body": "Test Content", "parentCommentId": 0 }';
// Send a post request
$ Http-> post ($ msg); echo 'OK ';

The sending process may be slow. please wait.

Finally, I don't mind if you use this blog to send comments for testing, but pay attention to the terms.

Accept, this blog POST undertakes "php uses socket to send GET, POST requests", to use the encapsulated Http class, consider how to use php scripts to blog...

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.