我想让php应用程序使用REST在wordpress.com上创建post。
我使用以下代码:
<?php
$curl = curl_init( 'https://public-api.wordpress.com/oauth2/token' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'client_id' => 12345,
'redirect_uri' => 'http://example.com/wp/test.php',
'client_secret' => 'L8RvNFqyzvqh25P726jl0XxSLGBOlVWDaxxxxxcxxxxxxx',
'code' => $_GET['code'], // The code fromthe previous request
'grant_type' => 'authorization_code'
) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
$secret = json_decode($auth);
$access_token = $secret->access_token;
$post = array(
'title'=>'Hello World',
'content'=>'Hello. I am a test post. I was created by
the API',
'date'=>date('YmdHis'),
'categories'=>'API','tags=tests'
);
$post = http_build_query($post);
$apicall = "https://public-api.wordpress.com/rest/v1/sites/mysite.wordpress.com/posts/new";
$ch = curl_init($apicall);
curl_setopt($ch, CURLOPT_HTTPHEADER, array
('authorization: Bearer ' . $access_token,"Content-Type: application/x-www-form-urlencoded;
charset=utf-8"));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$return = curl_exec($ch);
echo "<pre>";
print_r($return);
exit;
?>但我知道这个错误:
{“错误”:“未经授权”,“消息”:“用户不能发布帖子”}
能帮我吗?
谢谢
发布于 2018-02-20 21:17:48
创建帖子的标准方法是使用曲奇饼和nonce。
然而,我找到了一种更简单的方法。
admin和密码admin创建wordpress用户(这两种凭证都是不安全的,仅用于演示目的)请注意,在上面的示例中,使用了REST的版本2。
发布于 2020-12-22 05:42:34
答案是正确的,我们可以使用“Basic”插件来发出Rest请求。
但是,@vallez想在wordpress.com网站上创建一个帖子。
wordpress.com为身份验证提供了oAuth支持。
最近,我创建了一个关于使用oAuth在wordpress.com上创建帖子的帖子。你可以在使用wordpress.com和Rest在oAuth站点上创建帖子上阅读这篇文章
下面是使用oAuth在wordpress.com上成功创建post的步骤。
步骤1:添加身份验证细节以获取auth密钥。
$auth_args = array(
'username' => '',
'password' => '',
'client_id' => '',
'client_secret' => '',
'grant_type' => 'password', // Keep this as it is.
);
$access_key = get_access_key( $auth_args );下面是返回访问键的函数get_access_key()。
步骤2:获取访问密钥。
/**
* Get Access Key.
*
* @param array $args Auth arguments.
* @return mixed Auth response.
*/
function get_access_key( $args ) {
// Access Token.
$curl = curl_init( 'https://public-api.wordpress.com/oauth2/token' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $args );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
$auth = json_decode($auth);
// Access Key.
return $auth->access_token;
}步骤3:设置post参数并传递给它,创建post。
$post_args = array(
'title' => 'Test Post with oAuth',
'content' => 'Test post content goes here..',
'tags' => 'tests',
'post_status' => 'draft',
'categories' => 'API',
);步骤4:使用访问键创建一个post。
现在,我们有了access键和create参数。因此,让我们将它们传递给函数create_post()。
create_post( $access_key, $post_args );步骤5:创建带有访问键的post。
/**
* Create post with access key.
*
* @param string $access_key Access key.
* @param array $post_args Post arguments.
* @return mixed Post response.
*/
function create_post( $access_key, $post_args )
{
$options = array (
'http' => array(
'ignore_errors' => true,
'method' => 'POST',
'header' => array(
0 => 'authorization: Bearer ' . $access_key,
1 => 'Content-Type: application/x-www-form-urlencoded',
),
'content' => http_build_query( $post_args ),
),
);
$context = stream_context_create( $options );
$response = file_get_contents(
'https://public-api.wordpress.com/rest/v1/sites/YOURSITEID/posts/new/',
false,
$context
);
return json_decode( $response );
}https://stackoverflow.com/questions/35765474
复制相似问题