What am I missing from my wordpress wp-api test da

2019-06-26 08:31发布

I am testing out using wp-api, I am very excited to use this by the way. Here is what I have done.

  1. installed wp-api and verified that I can get from the website using cURL
  2. Installed basic authentication, created an editor level account for this testing.
  3. I have tried to update a post using cURL and Postman in chrome. my headers: -Content-Type: application/javascript -data in a file I am not sure if there are other headers that I am missing. Both cURL and postman handle username and password in basic authentication for me.

my JSON

{  
    "title":"Hello Updated World!",
    "content_raw":"Howdy updated content.",
    "date":"2013-04-01T14:00:00+10:00"
}

straight from the examples in wp-api docs

the error I keep getting is

[
    {
        "code": "json_missing_callback_param",
        "message": "Missing parameter data"
    }
]

Looking at the source code on github it looks like a required parameter is missing

according to the documentation title and content raw seem to be the only required fields. I would love to know what I am missing if you have any suggestion. Thanks.

2条回答
闹够了就滚
2楼-- · 2019-06-26 08:53

Here's the code I use to call a local WP-API with PHP/cURL:

$endpoint = 'http://localhost/my-site/wp-json/wp/posts';
$username = 'test-user';
$password = 'test-pass';

$data = array(
    'title'         => $post_title,
    'content_raw'   => $post_content,
    'status'        => 'publish',
);
$json_data = json_encode($data);

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json_data),
));

$result = json_decode(curl_exec($ch));

To update this post, I would change my data to:

$data = array(
    'ID'            => $post_id,
    'title'         => $new_title,
    'content_raw'   => $new_content,
);

ID is a required field when updating.

查看更多
Luminary・发光体
3楼-- · 2019-06-26 08:57

You can also try this plugin for making use of Wordpress webservices and by using it you can also create your custom webservice too :

https://wordpress.org/plugins/json-api/

查看更多
登录 后发表回答