如何使用WordPress REST API发布文章?我可以使用以下PHP代码编辑帖子,我正在使用基本身份验证。
require( dirname(__FILE__) . '/wp-load.php' );
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ),
);
// Edit post
$url = rest_url( 'wp/v2/posts/1' );
$data = array(
'title' => 'Test API Edit'
);
$response = wp_remote_post( $url, array (
'method' => 'POST',
'headers' => $headers,
'body' => $data
) );
print_r($response);
但我不能发表新文章
<?php
require( dirname(__FILE__) . '/wp-load.php' );
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ),
);
// post new article
$url = rest_url( 'wp/v2/create_post/' );
$data = array(
'title' => 'Post from API',
'content' => 'test contents'
);
$response = wp_remote_post( $url, array (
'method' => 'POST',
'headers' => $headers,
'body' => $data
) );
print_r($response);
返回始终为No route was found matching the URL and request method
有什么建议吗?