想通过WP-API wordpress发布帖子吗

想通过WP-API wordpress发布帖子吗,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我想在wordpress中通过API调用发布帖子,而不使用管理面板,我已使用http auth插件将授权管理到wordpress中,并使用以下代码更改.htaccess文件: RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule .* - [e=HTTP_AUTHORIZATION:%1] 我的剧本是这样的: <?php $process = curl_init('http://somewhereinsouth.com/wp-json/w

我想在wordpress中通过API调用发布帖子,而不使用管理面板,我已使用http auth插件将授权管理到wordpress中,并使用以下代码更改.htaccess文件:

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
我的剧本是这样的:

<?php
$process = curl_init('http://somewhereinsouth.com/wp-json/wp/v2/posts');
$data = array('slug' => 'welcome-to-the-jungle' , 'title' => 'Welcome to the Jungle' , 'content' => 'We welcome to you a better journey at the jungle.', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
$headers =  array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string),
    'Authorization' .'Basic'.base64_encode('admin:Best@xy123'));
// create the options starting with basic authentication
// curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "POST");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
// process the request
$return = curl_exec($process);
curl_close($process);
// This buit is to show you on the screen what the data looks like returned and then decoded for PHP use
echo '<h2>Results</h2>';
print_r($return);
echo '<h2>Decoded</h2>';
$result = json_decode($return, true);
print_r($result);

您能否尝试将您的
管理员:密码
作为curl
--user
而不是标题传递,请参阅
--user
的更多内容您能否在上面的代码中共享代码片段,如何使用--user。您已经注释掉了
CURLOPT_USERPWD
,这不起作用吗?我已经解决了这个问题,请告诉我您是否可以建议如何通过API将功能图像添加到post中。您需要将图像传递到不同的端点,即
/wp json/wp/v2/media
,并将其附加到post id。阅读更多信息,您可以尝试将
admin:password
作为curl
--user
而不是标头传递吗,请在这里查看更多关于
--user
您能否在上面的代码中共享代码片段,如何使用--user。您已经注释掉了
CURLOPT_USERPWD
,这不起作用吗?我已经解决了这个问题,请告诉我您是否可以建议如何通过API将功能图像添加到post。您需要将图像传递到不同的端点,即
/wp json/wp/v2/media
,并将其附加到post id。请阅读此处的详细信息
<h2>Results</h2><html>
                            <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                                <title>BestViewsReviews | Restricted Site</title>
                                <link rel="stylesheet" href="http://somewhereinsouth.com/wp-content/plugins/http-auth/frontend/css/style.min.css" type="text/css">
                            </head>
                            <body class="http-restricted">
                                <p>You are not allowed to perform this action.</p>
                            </body>
                     </html><h2>Decoded</h2>