Skip to content

Instantly share code, notes, and snippets.

@danielstrelec
Created February 10, 2025 13:48
Show Gist options
  • Save danielstrelec/7b58145572f81e26b11a8e3a1ccd70c4 to your computer and use it in GitHub Desktop.
Save danielstrelec/7b58145572f81e26b11a8e3a1ccd70c4 to your computer and use it in GitHub Desktop.
<?php
$rest_api_url = "http://wordpress.test/wp-json/wp/v2/posts";
$username = 'test';
$password = 'test';
$my_post = json_encode([
'title' => 'Název postu',
'content' => 'Obsah postu',
'excerpt' => 'Perex postu',
'status' => 'publish'
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rest_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $my_post);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($my_post),
'Authorization: Basic ' . base64_encode($username . ':' . $password),
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode( $result, true );
curl_close($ch);
echo 'id vloženého postu: ' . $result['id'];
echo '<pre>';
print_r( $result );
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment