事实上,我在发帖方面没有问题。我需要在php中使用REST API创建帖子file_get_contents
.
这是我的代码:
json_decode(
file_get_contents(
"http://my-site.com/wp-json/wp/v2/posts",
false,
stream_context_create(
array(
"http" => array(
"header" => "Content-type: text/json\\r\\n",
"method" => "POST",
"content" => json_encode(
array(
"slug" => "article-2",
"title" => "Article 2",
"content" => "Created with rest api",
)
)
),
\'ssl\' => array(
\'verify_peer\' => false,
\'verify_peer_name\' => false
)
)
)
),
true
);
我得到这个错误:
failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
因此,我尝试使用真实的wp用户名和密码进行身份验证:
"http" => array(
"header" => "Content-type: text/json\\r\\n"."Authorization: Basic " . base64_encode("$username:$password"),
"method" => "POST",
"content" => json_encode(
array(
"slug" => "article-2",
"title" => "Article 2",
"content" => "Created with rest api",
)
)
)
但结果是一样的。
如何使用REST API从php插入帖子file_get_contents
?