如何利用http协议发布博文评论,
如何利用http协议发布博客园博文评论,
先给大家介绍下实现原理:
给博文提交评论的实质就是通过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方法需要重新写一下。
public function post($body) { $this->setLine('POST'); // 重新设置content-type $this->setHeader('Content-Type:application/json; charset=UTF-8'); // 跳过setBody方法 // $this->setBody($body); $this->body[]=$body; // 计算content-length $this->setHeader('Content-length: ' . strlen($this->body[0])); $this->request(); return $this->response; }
在重新修改Http类后,我们便可以完成本文的主要代码的编写了。虽然按理论来说,设置头信息时只要设置cookie值就好了,但是将最好将头信息全部发送以提高成功率。
在发送评论之前,看一下发送的参数:
我们只需要将想要发送的内容填写到"body"后面就可以了。
代码部分:
最后是本文的主要代码:
<?php require "http.class.php"; $http=new Http('http://www.cnblogs.com/mvc/PostComment/Add.aspx'); //设置头信息 $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=C8013C91E54C151DEDA30E2C1E842982338C9054A8BB8639AC2DAB7578445BF1DF5BC49D39D8BE5FDAC33541CE4E4FA386CFD3F946EA1D79D1E34809A4CCBD7488A15641AEF685A6258CF3F03597BCAF50049F8C95A310076677598990FB2E4FB1E9671A; _5t_trace_sid=84a9ed0b086c2c127551cf911bec7b1d; _5t_trace_tms=1; _gat=1'); $http->setHeader('Pragma:no-cache'); $http->setHeader('Cache-Control:no-cache'); //设置请求体信息 $msg='{"blogApp":"DeanChopper","postId":4688667,"body":"测试内容","parentCommentId":0}'; //发送post请求 $http->post($msg); echo '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

AI Hentai Generator
Generate AI Hentai for free.

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

As a popular social e-commerce platform, Xiaohongshu’s user comments are an important interactive link. Many users find that on Xiaohongshu, the latest comments often appear at the bottom of the comment area. Why is this? This article will delve into the sorting mechanism of Xiaohongshu’s comment area and share some tips for ranking comments at the front. 1. Why are the latest comments on Xiaohongshu at the bottom? The default sorting method of the Xiaohongshu comment area is to display comments in chronological order, that is, the latest comments will be displayed at the bottom of the comment area. This sorting method helps users easily view the latest comments and obtain real-time information. Xiaohongshu encourages interaction between users and encourages users to actively participate in comments by placing the latest comments at the bottom of the page. This design allows users to browse other users’ comments,

People enjoy posting videos, browsing videos, commenting and interacting on Douyin. But in the comment interaction, sometimes we may make some inappropriate remarks or make mistakes and want to delete these comments. So how to delete my own comments on Douyin? 1. How to delete my comments on Douyin? 1. Log in to Douyin App and find the video with the comment you want to delete. 2. At the bottom of the video page, find a list of all comments. 3. Find your comment, click the heart icon (like) on the right side of the comment, then click the delete icon (trash can) to confirm deletion. 4. If you @ other people in the comment, you will receive a prompt when deleting the comment: "After deleting this comment, the person @ will not receive the notification." If you want the person @ to receive the notification, you can Cancel @ first,

Xiaohongshu is a popular social e-commerce platform where users can share shopping tips, recommend products and exchange shopping experiences. Some users found that comments they posted on Xiaohongshu were always displayed at the bottom, which confused them. This article will explore why Xiaohongshu’s comments arrangement causes user comments to appear at the bottom, and introduce how to modify the position of comments on Xiaohongshu. 1. Why are Xiaohongshu’s own comments at the bottom? The display order of comments on Xiaohongshu is usually based on the chronological order of the comments. If your comment is posted after another user has commented, it will appear at the bottom. This is Xiaohongshu’s default comment sorting method, which is designed to maintain order and clarity in the comment area. Xiaohongshu may based on the content of the comments

Xiaohongshu is a popular social media platform where users can share their lives and exchange experiences. Some users will find that when they comment on other people's posts, they cannot see it. So, why can’t others see my comments on Xiaohongshu? This article will explore this issue in detail to help you resolve your confusion. 1. Why can’t others see my comments on Xiaohongshu? Delayed review: Xiaohongshu review content has been reviewed and may be temporarily hidden due to containing sensitive words or illegal content. In this case, the other party may not be able to see the comment temporarily after it is posted. Generally, these comments will reappear once moderation is complete. Account anomalies may cause comments posted by commenters not to be seen by the other party, even if the comments are successfully posted. Such abnormal situations include accounts being banned or functions limited.

Xiaohongshu is a popular social e-commerce platform where users can share their shopping experiences and recommend products. Some users reported that they were troubled by the fact that the images they uploaded in their comments were not displayed properly. This article will help you find out why images in Xiaohongshu comments cannot be displayed and provide solutions. 1. Why can’t pictures be displayed in Xiaohongshu comments? Image formats may not be supported: Xiaohongshu’s comment function only supports limited image formats. If the image format you upload is not supported, the image may not be displayed. It is recommended that you try other formats, such as JPG, PNG, etc. The size of images uploaded in comments may be limited by Xiaohongshu. If the size of your image is too large, it may not be displayed properly. It is recommended that you try pressing

Xiaohongshu is a content-rich and interactive social platform where users can post and comment on various contents. Sometimes we may encounter situations where our comments are deleted by bloggers. So, will the blogger be reminded if a Xiaohongshu comment is deleted? What should we do if it is deleted? 1. Will the blogger be reminded if a Xiaohongshu comment is deleted? After a Xiaohongshu comment is deleted by the blogger, the system will provide a notification to the user. Users can check the notification in Xiaohongshu’s message center to learn why their comments were deleted. Users can also find their deleted comments in the comment record on their personal homepage and view the relevant situation. 2. What should I do if my comments in Xiaohongshu are deleted? 1. Stay calm: First of all, we need to stay calm and don’t panic or get angry. Bo

In the current digital age, Xiaohongshu has become the main platform for many people to share their lives and obtain information. Sometimes, we may need to delete historical comments on Xiaohongshu, or encounter a situation where the comments are successful but cannot be seen. This article will focus on these two issues, providing detailed operation methods and possible causes. 1. How to delete historical comments on Xiaohongshu? To delete historical comments on Xiaohongshu, you can usually follow the following steps: 1. Find the comments: In the Xiaohongshu APP, find the comments you posted. Comments usually appear below the note or in the comments section. 2. Long press on a comment: Use your finger to long press on your comment until the delete option appears. 3. Confirm deletion: In the pop-up deletion confirmation dialog box, click the "OK" or "Delete" button to delete

On Xiaohongshu, users can browse a variety of beauty, fashion, travel and other content, while also expressing their own opinions and comments. Some users encountered the problem of how to pin comments when using the Xiaohongshu computer version. 1. How to pin comments to the top of Xiaohongshu on the computer? 1. Open the computer version of Xiaohongshu and enter the post or update you want to comment on. 2. In the comment area, find the comment you want to pin. 3. Click on the comment and you will see a "Pin" button. 4. Click the "Pin" button, and the comment will appear at the top of the comment area and become a pinned comment. It should be noted that the Xiaohongshu computer version currently only supports pinning your own comments, and the pinning function is only valid in the comment area of posts or updates, and cannot be pinned on topic pages or global comment areas. two,
