首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rest创建post wordpress.com

使用rest创建post wordpress.com
EN

Stack Overflow用户
提问于 2016-03-03 07:14:32
回答 2查看 19.3K关注 0票数 4

我想让php应用程序使用REST在wordpress.com上创建post。

我使用以下代码:

代码语言:javascript
复制
<?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;

?>

但我知道这个错误:

{“错误”:“未经授权”,“消息”:“用户不能发布帖子”}

能帮我吗?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2018-02-20 21:17:48

创建帖子的标准方法是使用曲奇饼和nonce。

然而,我找到了一种更简单的方法。

  1. 安装基本-8月插件到您的wordpress。
  2. 使用用户名admin和密码admin创建wordpress用户(这两种凭证都是不安全的,仅用于演示目的)
  3. 使用代码创建post: $username = 'admin';$password = 'admin';$rest_api_url =‘http://my-wordpress-site.com/wp-json/wp/v2/posts";$data_string = json_encode(’标题‘=> 'My title',’=>‘=> 'My content','status’=> 'publish',$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$rest_api_url);curl_setopt($ch,$rest_api_url,1);json_encode(,,);curl_setopt($ch,CURLOPT_HTTPHEADER,‘Content: application/json’),‘Content:’。strlen($data_string),“授权:基本”。base64_encode($username .“:”。( $password),);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);$result = curl_exec($ch);curl_close($ch);if ($result) { //…}{ // .}

请注意,在上面的示例中,使用了REST的版本2。

票数 8
EN

Stack Overflow用户

发布于 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密钥。

代码语言:javascript
复制
$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:获取访问密钥。

代码语言:javascript
复制
/**
 * 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。

代码语言:javascript
复制
$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()。

代码语言:javascript
复制
create_post( $access_key, $post_args );

步骤5:创建带有访问键的post。

代码语言:javascript
复制
/**
 * 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 );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35765474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档