Created
February 10, 2025 13:48
-
-
Save danielstrelec/7b58145572f81e26b11a8e3a1ccd70c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$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