抱歉,您不能以该用户身份使用curl请求php来创建帖子

问题描述 投票:0回答:1

我正在使用curl请求在wordpress网站上发帖,两年来一切都工作正常,突然wordpresscurl停止工作。

1 我更新了 htaccess 并尝试了所有可能的解决方案,但没有运气

2 我安装了 wp 基本身份验证,但它不起作用

3 我安装了 jwt 身份验证插件,但没有运气

4 我将每个角色分配给用户并更新密码,但不幸的是 WordPress 未验证远程请求。

5 在邮递员中显示“

{
    "code": "rest_cannot_create",
    "message": "Sorry, you are not allowed to create posts as this user.",
    "data": {
        "status": 401
    }
}

代码:

function do_wordpress_curl($data,$url,$header,$wp_rest_username,$wp_rest_password){
        $header_array = array();
        
        $auth = 'Authorization: Basic ' . base64_encode( $wp_rest_username . ':' . $wp_rest_password );

        array_push($header_array,$auth);
       
        if($header <> null)
            array_push($header_array,stripslashes($header));
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        // Receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Set Custom headers ...
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array );

        $server_output = curl_exec($ch);
        var_dump($server_output);
        // die();

        $header_data= curl_getinfo($ch);
        
        curl_close ($ch);
        $response = json_decode($server_output, true);
        return $response;
      
    }
php wordpress curl rest
1个回答
0
投票

我已将以下代码添加到 .htaccess 并添加插件(JSON 基本身份验证),然后它开始工作

RewriteCond %{HTTP:Authorization} ^(.)

RewriteRule ^(.) - [E=HTTP_AUTHORIZATION:%1]

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
© www.soinside.com 2019 - 2024. All rights reserved.