Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP Rest API不允许通过Laravel请求发布匿名评论_Wordpress_Wordpress Rest Api - Fatal编程技术网

Wordpress WP Rest API不允许通过Laravel请求发布匿名评论

Wordpress WP Rest API不允许通过Laravel请求发布匿名评论,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我正在使用WP-restapi在一个Laravel项目中获取帖子。它不允许针对特定帖子发表匿名评论。我还更新了functions.php 以下是错误: {"code":"rest_cannot_read_post","message":"Sorry, you are not allowed to read the post for this comment.","data":{"sta (truncated...) Api类别: public static function postComme

我正在使用WP-restapi在一个Laravel项目中获取帖子。它不允许针对特定帖子发表匿名评论。我还更新了functions.php

以下是错误:

{"code":"rest_cannot_read_post","message":"Sorry, you are not allowed to read the post for this comment.","data":{"sta (truncated...)
Api类别:

public static function postComments($postId, $author_name, $author_email, $content){

    $url = config::get('app.WP_BASE_URL') . '/wp-json/wp/v2/comments?author_name='. $author_name.'&author_email='. $author_email.'&content='. $content.'&post='. $postId;

    $params = [
        'post' => $postId,
        'author_email' => $author_email,
        'author_name' => $author_name,
        'content' => $content
    ];

    $data = Wpapi::curlPostRequest($url, $params);

    return $data;

} 
控制器:

public function postComment(Request $request){

  $id = $request->input('id');

  $data = $request->validate([
  'name' => 'required|max:255',
  'email' => 'required',
  'comment' => 'required',
  ]);
$author_email = $data['email'];
$author_name = $data['name'];
$content = $data['comment'];

      $postComment = Wpapi::postComments($id, $author_name, $author_email, $content );

    if($postComment){
      echo "Success";
    }
    else {
      echo "Failure";
    }

}解决方案1:

与GET请求一样,您也可以使用POST请求发布数据。您需要做的是通过POST API调用传递授权头
您可以在此处获得有关授权机制的更多详细信息:

标题:

Authorization:Bearer <token>
Content-Type:application/json

然后尝试运行您已经编写的代码段。我建议使用解决方案1。

解决方案1:

与GET请求一样,您也可以使用POST请求发布数据。您需要做的是通过POST API调用传递授权头
您可以在此处获得有关授权机制的更多详细信息:

标题:

Authorization:Bearer <token>
Content-Type:application/json

然后尝试运行您已经编写的代码段。我建议使用解决方案1。

Hey Kashif,您是否通过Post请求传递任何授权?不,我没有通过Post请求传递任何授权。发布评论需要授权。你能添加你正在尝试做的请求的spinet吗?我已经添加了代码段Hey Kashif,你是否通过Post请求传递任何授权?不,我没有通过Post请求传递任何授权。发布评论需要授权。你能添加你想要做的请求的尖刺吗?我已经添加了片段,很高兴听到@KashifRaza。谢谢!如何通过metada?我得到了一份401,上面写着“对不起,你不允许编辑***自定义字段”。…很高兴听到这个消息@KashifRaza。谢谢!如何通过metada?我得到了一个401,上面写着“对不起,你不允许编辑***自定义字段。”。。。