Create new posts on Wordpress Site via REST API using PHP Code
<?php
// API URL for creating a new post
$url = '
https://your_domain.com/wp-json/wp/v2/posts
';
// Data for the new post
$data = [ 'title' => 'Hello World', 'status' => 'publish', 'content' => 'This is the post content.', 'excerpt' => 'This is a short excerpt.', ];
// Encode data to JSON
$data_string = json_encode($data);
// Setup the cURL session
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Basic ' . base64_encode('your_username:your_application_password'), 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) ]);
// Execute the request
$result = curl_exec($ch);
curl_close($ch);
// Output the results
echo $result; ?>
We used the above code for the purpose of creating articles on our appvipo page!