wordpress rest api授权

时间:2018-08-08 17:19:36

标签: php wordpress api wordpress-rest-api

嗨,我使用此代码通过api在wordpress中上传图片

我的代码:

<?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 ) );

?>

但是当我使用此代码时,显示错误401“ rest_cannot_create”

我使用正确的用户名和密码(wordpress管理面板的用户名和密码)

是否有另一种授权rest api wordpress的方式?

1 个答案:

答案 0 :(得分:0)

尝试将其添加到htaccess文件中:

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