admin管理员组文章数量:1122832
I'm trying to create a WordPress post using the REST API, OAuth, and cURL. However, I'm running into some trouble.
I'm able to create a blank post (no title, slug, etc) using the following code:
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
This works as expected - authorizes successfully, and creates a blank post in WordPress. However, when I go to set the title (or any other property) by adding
CURLOPT_POSTFIELDS => "title=testTitle",
I get an error:
{
"data": {
"code": "json_oauth1_signature_mismatch",
"message": "OAuth signature does not match",
"data": {
"status": 401
}
},
"headers": [],
"status": 401
}
What's interesting is that I'm able to set a title with success using Postman. Any ideas what I'm doing wrong? Any help would be greatly appreciated - been at this for hours.
I'm trying to create a WordPress post using the REST API, OAuth, and cURL. However, I'm running into some trouble.
I'm able to create a blank post (no title, slug, etc) using the following code:
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
This works as expected - authorizes successfully, and creates a blank post in WordPress. However, when I go to set the title (or any other property) by adding
CURLOPT_POSTFIELDS => "title=testTitle",
I get an error:
{
"data": {
"code": "json_oauth1_signature_mismatch",
"message": "OAuth signature does not match",
"data": {
"status": 401
}
},
"headers": [],
"status": 401
}
What's interesting is that I'm able to set a title with success using Postman. Any ideas what I'm doing wrong? Any help would be greatly appreciated - been at this for hours.
Share Improve this question edited Jun 8, 2017 at 23:04 Dave Romsey 17.9k11 gold badges55 silver badges70 bronze badges asked Jun 4, 2017 at 23:52 Dan PDan P 412 bronze badges1 Answer
Reset to default 0"The REST API uses JSON exclusively as the request and response format, including error responses."
-- WordPress REST API Reference
I would try explicitly setting the content type/length and json encode your data with something like this (assuming you need to specify all the options you did in your code provided):
$data = array(
'title' => 'testTitle',
'content' => '',
);
$data = json_encode( $data );
$header = array(
buildAuthorizationHeader( $oauth ),
'Content-Type: application/json',
'Content-Length: ' . strlen( $data ),
'Expect:',
);
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => $data,
);
本文标签: phpCreating a post with the REST APIcurl and oauth returning 401 error
版权声明:本文标题:php - Creating a post with the REST API, curl and oauth returning 401 error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287236a1927832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论