利用http协议公布博客园博文评论
利用http协议发布博客园博文评论
本博文承接《php 利用socket发送GET,POST请求》,要利用上文封装好的Http类,考虑如何通过php脚本给博客园的博文提交评论。
原理:
在做这件事前,我们首先要明白,给博文提交评论的实质就是通过http协议服务器发送一个post请求。在发布评论前,我们需要做什么呢?对,是必须要登录的。但登录是另一件事情,我们这里先不讨论。用户登录后,服务器给客户端设置一个cookie。http是无状态的。也就是说客户端向服务器发送请求后,服务器返回响应。一次通信完成。服务器不会记得刚才是谁向自己发送请求。所以客户端需要拿着服务器给自己设定好的cookie向服务器发送请求并告知服务器自己的身份,服务器根据cookie产生响应。原理就是如此简单,来看看我们的实战环节。
准备工作:
为了完成本次测试,我又注册了一个博客园小号(DeanHuangChopper),登录博客园后,打开我博客(DeanChopper),随便打开我的一篇博文,例如《结合php ob函数理解缓冲机制》一文(我用的是火狐浏览器,最大优点是向服务器发送什么参数可以很直观地看出来了),打开开发者选项,准备记录发送评论的过程。我随便写一评论,发表评论。这一次请求通过已经开发者选项记录下来。
我们只需要关注请求头就好了。
很明显,我们通过Http类的setHeader()方法设置好请求头信息,通过post()方法发送就可以了。我们先仔细分析下请求头信息。Host和Content-type,Contetn-length会默认通过http的一些方法设置好,我们可以不添加。但必须注意的是Content-type类型是"application/json; charset=UTF-8",有别于Http默认设置的“application/x-www-form-urlencoded”。请求体是json形式,而非数组形式,因而原来的Http post方法需要重新写一下。
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> post(<span style="color: #800080;">$body</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->setLine('POST'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 重新设置content-type</span> <span style="color: #800080;">$this</span>->setHeader('Content-Type:application/json; charset=UTF-8'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 跳过setBody方法 // $this->setBody($body);</span> <span style="color: #800080;">$this</span>->body[]=<span style="color: #800080;">$body</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 计算content-length</span> <span style="color: #800080;">$this</span>->setHeader('Content-length: ' . <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$this</span>->body[0<span style="color: #000000;">])); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">request(); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">response; }</span>
在重新修改Http类后,我们便可以完成本文的主要代码的编写了。虽然按理论来说,设置头信息时只要设置cookie值就好了,但是将最好将头信息全部发送以提高成功率。
在发送评论之前,看一下发送的参数:
我们只需要将想要发送的内容填写到"body"后面就可以了。
代码部分:
最后是本文的主要代码:
<span style="color: #000000;">php </span><span style="color: #0000ff;">require</span> "http.class.php"<span style="color: #000000;">;</span><span style="color: #800080;">$http</span>=<span style="color: #0000ff;">new</span> Http('http://www.cnblogs.com/mvc/PostComment/Add.aspx'<span style="color: #000000;">);</span>//设置头信息<span style="color: #800080;">$http</span>->setHeader('Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Accept-Encoding:gzip, deflate'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('X-Requested-With:XMLHttpRequest'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Referer:http://www.cnblogs.com/DeanChopper/p/4688667.html'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Cookie:_ga=GA1.2.1359064105.1438444082; __gads=ID=e0c32fd6db6e2a6d:T=1438443900:S=ALNI_Mb6AAflcBD6gcdHgeE3IqVDJYnnjA; .CNBlogsCookie=C8013C91E54C151DEDA30E2C1E842982338C9054A8BB8639AC2DAB7578445BF1DF5BC49D39D8BE5FDAC33541CE4E4FA386CFD3F946EA1D79D1E34809A4CCBD7488A15641AEF685A6258CF3F03597BCAF50049F8C95A310076677598990FB2E4FB1E9671A; _5t_trace_sid=84a9ed0b086c2c127551cf911bec7b1d; _5t_trace_tms=1; _gat=1'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Pragma:no-cache'<span style="color: #000000;">);</span><span style="color: #800080;">$http</span>->setHeader('Cache-Control:no-cache'<span style="color: #000000;">);<br>//设置请求体信息<br></span><span style="color: #800080;">$msg</span>='{"blogApp":"DeanChopper","postId":4688667,"body":"测试内容","parentCommentId":0}'<span style="color: #000000;">;<br>//发送post请求<br></span><span style="color: #800080;">$http</span>->post(<span style="color: #800080;">$msg</span><span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> 'OK';
发送过程可能有点慢,请耐心等待。
最后说明,我不介意博友拿我本篇博文做发送评论测试,但请注意用语。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Common network communication and security problems and solutions in C# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.
