1

I want to publish posts through an API call in wordpress without using the admin panel, I have used the http-auth plugin to manage the authorization into wordpress, also change the .htaccess file with the below code:

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

and my script is this:

<?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);

And now I get the following error while posting the posts at my website using this script:

<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>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Manish Agarwal
  • 166
  • 2
  • 12
  • Can you try pass your `admin:password` as a curl `--user` rather than a header, see more here on `--user` https://stackoverflow.com/questions/36292406/what-does-user-mean-with-curl – designtocode Mar 08 '19 at 07:19
  • can you please share snippet, how to use --user in above code. – Manish Agarwal Mar 08 '19 at 08:31
  • You have commented out `CURLOPT_USERPWD`, did this not work? – designtocode Mar 08 '19 at 08:53
  • i have solved this, can you please tell me know if you can suggest how to add feature images into post through the API. – Manish Agarwal Mar 08 '19 at 10:49
  • You need to pass the image in a different endpoint i.e. `/wp-json/wp/v2/media` and attach it to the post id. Read more here https://stackoverflow.com/questions/33103707/wp-rest-api-how-to-upload-featured-image – designtocode Mar 08 '19 at 13:15

0 Answers0