wordpress rest api Authorization

一个人想着一个人 提交于 2020-01-06 05:44:29

问题


hi i use this code for upload image in wordpress with api

my code :

<?php

$file = $_FILES["RESULT_FileUpload-6"]["tmp_name"];
$url = 'http://tst.com/wp-json/wp/v2/media/';
$ch = curl_init();
$username = 'username';
$password = '123456';

curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Disposition: form-data; filename="'.$_FILES["RESULT_FileUpload-6"]["name"].'"',
    'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
] );
$result = curl_exec( $ch );
curl_close( $ch );
print_r( json_decode( $result ) );

?>

but when i use this code show error 401 "rest_cannot_create"

i use username and password correct (username and password for wordpress admin panel)

Is there another way for Authorization rest api wordpress??


回答1:


try to add this in the htaccess file:

RewriteRule ^index\.php$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


来源:https://stackoverflow.com/questions/51752291/wordpress-rest-api-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!