<?php
function wpAutoPoster($token,$endpointUrl,$data){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $endpointUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'accept: application/json',
'Authorization: Bearer '.$token
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
$jwtAuthToken="xxx";
$endpointUrl="https://www.example.com/wp-json/wp/v2/tags";
$data=json_encode(['name'=>'my new tag 1']);
$response=json_decode(wpAutoPoster($jwtAuthToken,$endpointUrl,$data));
var_dump($response);
The above works as expected.
Now, to create categories, just change the following 2 lines:
$endpointUrl="https://www.example.com/wp-json/wp/v2/categories";
$data=json_encode(['name'=>'my new category 1'])