【问题标题】:How to fix 'rest_invalid_param' when tried to post a comment using wordpress API尝试使用 wordpress API 发表评论时如何修复“rest_invalid_param”
【发布时间】:2020-01-12 14:43:59
【问题描述】:

我正在尝试使用 cURL 对博客文章发表评论,但最终出现“rest_invalid_param”错误。

我尝试在邮递员的 cURL 中运行相同的 URL,它运行良好。我不知道我哪里出错了。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/aapta-blog/wp-json/wp/v2/comments");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = array(
'author_email' => urlencode($user_email),
'author_name' => urlencode($user_name),
'content' => urlencode($content),
'post' => urlencode($post_id)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
die();

我什至尝试过 json_encode($data),但仍然出现错误。 我在邮递员中传递的是http://example.com/aapta-blog/wp-json/wp/v2/comments?author_email=novusflos.web@gmail.com&author_name=Danu&content=Test from postman2&post=38

错误:

{
  code: "rest_invalid_param",
  message: "Invalid parameter(s): post",
  data: {
    status: 400,
    params: {
      post: "post is not of type integer."
    }
  }
}

【问题讨论】:

    标签: php wordpress codeigniter curl


    【解决方案1】:

    URL 编码可能会在 cURL 中出错。不妨试试这个。

    $data = json_encode( 
    
         array(
          'author_email' => $user_email,
          'author_name' => $user_name,
          'content' => $content,
          'post' => $post_id
         )
    );
    
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://example.com/aapta-blog/wp-json/wp/v2/comments");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    curl_close($ch);
    print_r($result);
    die();
    

    【讨论】:

    • 不工作。帖子 ID 错误。 { code: "rest_comment_invalid_post_id", message: "对不起,您不能在没有帖子的情况下创建此评论。", data: { status: 403 } }
    • post_id 传到哪里去了?你能分享你的那部分代码吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多