问题
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