wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

2,433次阅读
没有评论

今天遇到了一个问题,愣是看的库源码才搞明白,原来是作者写的库有问题,一会我给作者提个issue,嘿嘿😝。

前言时刻:

想打造一个wp主题评论机器人功能, 使用是wordpress_xmlrpc这个库时,可以直接对接 wordpress 中的 xmlrpc 接口,很是方便。评论方面有这个类WordPressComment,方法上有:

wordpress_xmlrpc.methods.comments.NewComment(post_id, comment)

  • post_id:是文章的id号
  • comment:是实例化的 WordPressComment 的对象,
  • 另外:NewComment是以上当前登录的账户进行评论,需验证账户密码权限,不可以自定义账户。

wordpress_xmlrpc.methods.comments.NewAnonymousComment(post_id, comment)

  • post_id:是文章的id号
  • comment:是实例化的 WordPressComment 的对象,
  • 提示:可以自定义账户邮箱进行评论,并且无需验证该评论账户的全权限。另外:还需要在wordpress后台的设置->讨论->取消勾选:”用户必须注册并登录才可以发表评论“

所以要打造一个wp自动评论机器人的话,那么肯定是首选 NewAnnonymousComment 方法,但是问题来了,因为作者写的坑等着我来了……

配置说明:

wordpress版本号:5.7.3版本

python3.8

wordpress_xmlrpc:2.3

问题

  def comment_robot(self, article_id, **kwargs):
        """ 评论机器人 """
        self.wp = Client(kwargs['user_web_url'], kwargs['user_web_account'], kwargs['user_web_pwd'])

        comment_obj = WordPressComment()
        comment_obj.content = 'ff厉害8822了666'
        comment_obj.status = 'hold'
        comment_obj.author = 'fwf'
        comment_obj.author_email = 'fwf@163.om'

        # 需要验证身份的
        # res = self.wp.call(comments.NewComment(article_id, comment_obj))

        # 无需验证身份,嘿嘿,牛皮我喜欢
        # res = self.wp.call(comments.NewAnonymousComment(article_id, '', '', article_id, comment_obj))
        res = self.wp.call(comments.NewAnonymousComment(article_id, comment_obj))
        pass

以上就是我遇到的问题代码,你发现你运行之后就会遇到一片红:

xmlrpc.client.Fault: <Fault 404: '文章ID无效。'>
或者是:
Invalid post ID.

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

开始我以为是是我语法写错了,然后我到作者写的官方文档上,和我写的是一样(后面的可以证明我这里写的代码是没问题的)

classwordpress_xmlrpc.methods.comments.NewAnonymousComment(post_id, comment):

Create a new comment on a post without authenticating.

NOTE: Requires support on the blog by setting the following filter in a plugin or theme:
add_filter( ‘xmlrpc_allow_anonymous_comments’, ‘__return_true’ );

Parameters:
post_id: The id of the post to add a comment to. comment: A WordPressComment instance with at least the content value set.
    Returns: ID of the newly-created comment (an integer).

1、分析wordpress源代码

那么怎么解决呢?

即便用了谷歌以及Stack Overflow,你也依然找不到答案,那么我只好去读作者写的源码(以前不敢读的原因是,语法没学好,但现在可以了😄)。

一读源码深似海,只缘身在此山中,读了一遍源码,我发现还是没搞明白问题出哪了,我想着是不是 wordpress 中的 xmlrpc 模块有问题呀,可能是两者之间的对接问题。

所以我又入坑了,去看wordpress的源码,找到了位于wordpress根目录下的xmlrpc.php文件,发现里面也没有啥东西,其实东西就在里面,是一个引用的文件,然而就是引用的文件里面有我想要的东西。

1.1 开启wordpress日志模式:

然后我网上搜索了一下 怎么开启 wordpress 的日志打印功能:

  • 找到wordpress根目录下的 wp-config.php------>找到 define( "WP_DEBUG", true );设置为true

  • ------>然后在下面在写入日志文件代码:define( "WP_DEBUG_LOG", true );这样就可开启debug模式,日志文件在根目录下的wp-conent文件夹中。

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

  • 点开这个日志文件:debug.log,然后电脑端运行一下代码,看看wp的日志打印,找出错误出在哪个文件中,对症下药。

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

2、分析wordpress_xmlrpc源代码

好了,终于知道问题出在那个文件了,我果断打开这个class-wp-xmlrpc-server.php文件,阅读代码。

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

然后你再看 wordpress_xmlrpc 源码,你会发现,NewAnonymousComment 类居然少传进去了三个参数,我嘞个去,作者也太大意了……,有可能是wordpess的版本较高导致的原因。

至于我是怎么看出它少传进去了三个参数,一点点看的,我就不详说了,有点杂。下面的(从上往下)分别是NewAnonymousComment类和NewComment类 继承的父类,此文件是位于 python 的wordpress_xmlrpc包里面 method文件下的comments.py中,如果找不到,就看运气了~

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

添加代码,其实也就是把我截图中的注释引掉就可。

def default_args(self, client):
    return ('', '', '')

看看结果:

wordpress-rpcxml-xmlrpc.client.Fault_文章ID无效-解决

搞定了,希望帮助到你,哈哈~

参考链接:

https://www.itbook5.com/2018/12/1982/

3
西园公子
版权声明:本站原创文章,由西园公子2021-07-16发表,共计2877字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...