Post blog post comments with the HTTP protocol

Source: Internet
Author: User
Tags http post
This blog post takes the PHP use socket to send Get,post request, to take advantage of the above encapsulated HTTP class, consider how to use PHP script to submit a blog post comments.

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 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. The principle is so simple, to see our actual combat link.

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 strlen ($this->body[0]));         Request for $this();         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=NewHttp (' 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 the request body information
$msg= ' {"Blogapp": "Deanchopper", "PostID": 4688667, "body": "Test Contents", "Parentcommentid": 0} ';
Send a POST request
$http->post ($msg);Echo' OK ';

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

Finally, I do not mind bo friends take me this blog post to send comment test, but please pay attention to the language.

The above describes the use of the HTTP protocol to publish blog post comments, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.