Skip to content
Advertisement

Sorry, you are not allowed to create posts as this user using curl request php

i am using curl request to make post in wordpress site every thing were working fine from 2 years suddenly wordpress curl stop working.

1 i updated the htaccess to and try every possible solution but no luck

2 i install wp basic authentication but it does not work

3 i install jwt authentication plugin but no luck

4 i assign every role to user and update password but no luck wordpress is not validating remote request.

5 in postman it show that “

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

Code:

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;
      
    }

Advertisement

Answer

i have added the following code to htaccess and add plugin (JSON Basic Authentication) then it start working

`RewriteCond %{HTTP:Authorization} ^(.)

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

SetEnvIf Authorization “(.*)” HTTP_AUTHORIZATION=$1`

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement