How to use the HTTP protocol to publish blog post comments, _php tutorials

Source: Internet
Author: User

How to use the HTTP protocol to publish blog post comments,


First of all introduce the implementation of the principle:

The essence of submitting a comment to a blog post is to send a POST request via the HTTP protocol server. What do we need to do before we post a comment? Yes, it has to be logged in. But login is another thing that we don't discuss here first. After the user logs in, the server sets a cookie to the client. HTTP is stateless. This means that the server returns a response after the client sends a request to the server. Once the communication is complete. The server will not remember who just sent the request to itself. So the client needs to hold the server to its own set of cookies to send a request to the server and inform the server of its own identity, the server based on the cookie response.

Preparatory work:

In order to complete this test, I also registered a blog Park trumpet (deanhuangchopper), log in to the blog park, open my Blog (deanchopper), casually open a blog post, such as "with PHP ob function Understanding buffer mechanism" article (I use the Firefox browser, The biggest advantage is that what parameters are sent to the server can be seen intuitively, open developer options and prepare to record the process of sending comments. I write a comment casually and comment. This request is recorded by the developer option.

All we need to do is focus on the request header.

Obviously, we set the request header information through the HTTP class's SetHeader () method and send it via the post () method. We carefully analyze the request header information first. Host and Content-type,contetn-length will default through some methods of HTTP settings, we can not add. But it must be noted that the Content-type type is "Application/json; Charset=utf-8 ", different from the HTTP default setting of" application/x-www-form-urlencoded ". The request body is in JSON form, not array form, so the original HTTP POST method needs to be re-written.

Public Function post ($body) {    $this->setline (' post ');  Reset Content-type  $this->setheader (' Content-type:application/json; Charset=utf-8 ');    Skip 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 we re-modify the HTTP class, we can finish writing the main code of this article. Although it's a good idea to set the cookie value when setting the header information, it's best to send all the header information to improve the success rate, as a theory.

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

We just need to fill in the content we want to send back to "body".

Code section:

Finally, the main code of this article:

<?phprequire "http.class.php"; $http =new http (' http://www.cnblogs.com/mvc/PostComment/Add.aspx ');//Set Header information $http- >setheader (' accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 '); $http->setheader (' Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 '), $http->setheader (' accept-encoding:gzip, deflate '); $http->setheader ( ' X-requested-with:xmlhttprequest '); $http->setheader (' referer:http://www.cnblogs.com/deanchopper/p/4688667. html '); $http->setheader (' cookie:_ga=ga1.2.1359064105.1438444082; __gads=id=e0c32fd6db6e2a6d:t=1438443900:s= Alni_mb6aaflcbd6gcdhgee3iqvdjynnja;. Cnblogscookie= c8013c91e54c151deda30e2c1e842982338c9054a8bb8639ac2dab7578445bf1df5bc49d39d8be5fdac33541ce4e4fa386cfd3f946ea1d79d1e34809a 4ccbd7488a15641aef685a6258cf3f03597bcaf50049f8c95a310076677598990fb2e4fb1e9671a; _5t_trace_sid=84a9ed0b086c2c127551cf911bec7b1d; _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 Contents", "Parentcommentid": 0} ';//Send POST request $http->post ($msg); Echo ' OK '; 

The sending process may be a bit slow, please wait patiently.

The above introduction is the whole content of this article, I hope you like it.

http://www.bkjia.com/PHPjc/1042692.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042692.html techarticle How to use the HTTP protocol to publish blog post comments, first introduce the implementation principle: the essence of submitting a comment to the blog is to send a POST request via the HTTP protocol server. In the hair ...

  • 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.