我正在尝试在用户登录时在wordpress中设置JWT。我以前没有用PHP进行JWT或发送API请求,但这是我到目前为止的事情:
function add_jwt($user,$password){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, get_site_url(null,'/wp-json/jwt-
auth/v1/token') );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,
"username={$user->$user_login}&password={$password}" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:
text/plain'));
$result=curl_exec ($ch);
var_error_log($result);
curl_close($curl);
return $user;
}
add_filter('wp_authenticate_user', 'add_jwt', 10, 2);
在向Postman输入原始数据时,它可以正常工作。但是,尝试将同一过程转换为PHP cURL会给我这个错误。
[08-Aug-2019 05:51:45 UTC] string(124) "{"code":"[jwt_auth]
empty_username","message":"<strong>ERROR<\/strong>: The username field is
empty.","data":{"status":403}}"