Last active
August 6, 2024 08:59
-
-
Save andrewahead4/489e6422feb5be901143 to your computer and use it in GitHub Desktop.
A simple post insert using WP REST API and PHP over basic authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/////////////////////////////////////////////////////////////////////////////////// | |
// // | |
// This is using a sample local WordPress Install and is not production safe // | |
// It uses the REST and Basic Auth plugins // | |
// // | |
/////////////////////////////////////////////////////////////////////////////////// | |
// setup user name and password | |
$username = 'admin'; | |
$password = 'password'; | |
// the standard end point for posts in an initialised Curl | |
$process = curl_init('http://blogimport.dev/wp-json/wp/v2/posts'); | |
// create an array of data to use, this is basic - see other examples for more complex inserts | |
$data = array('slug' => 'rest_insert' , 'title' => 'REST API insert' , 'content' => 'The content of our stuff', 'excerpt' => 'smaller' ); | |
$data_string = json_encode($data); | |
// 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, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
// 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); |
Use the simple jwt-auth plugin, but use this fork https://github.com/jonathan-dejong/simple-jwt-authentication/wiki/Documentation
does not work for https site
Worked for me. As is. On an HTTPS site.
Thanks Andrew.
Hello. thanks you. worked for me.
generate an application passwords and email credentials.
HTTPS
PHP 7.4.21 (cli) (built: Jul 2 2021 15:33:47) ( NTS )
Saludo, Andres.
Thanks Andrew works like magic 👍
This the one of the few examples on the web to show how to actually interact with WordPress rest api with PHP and curl :)
If you add also the fields categories, tags, meta and featured_media it becomes PERFECT!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anyone know of a snippet that does this using auth cookies instead of basic auth?