Takip et

WP REST API ve PHP ile WordPress uzaktan yazı ekleme

Merhaba, geçtiğimiz günlerde WP Rest API üzerinde testler yaparken basit bir yazı ekleme kombinasyonu oluşturdum, böylece wp rest api ile birlikte uzaktan wordpress sistemimize yeni yazı ekleme işlemi yapabiliriz. Dilerseniz başlayalım.

Adım 1: https://github.com/wp-api/basic-auth linki üzerinden basic-auth eklentisini indirip wordpress sistemimize kuralım.

Adım 2: Artık kod betiğimize geçebiliriz.

<?php

///////////////////////////////////////////////////////////////////////////////////
//                                                                               //
// Bu örnek bir öğreticidir, lütfen güvenlik detaylarınızı gözden geçiriniz      //
// REST ve Temel Kimlik Doğrulama eklentilerini kullanır                         //         //                                                                               //
///////////////////////////////////////////////////////////////////////////////////


// 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);

?>

HTTPS
PHP 7.4.21 (cli) (built: Jul 2 2024 15:33:47) ( NTS )

Soru, yorum ve görüşlerinizi yorum bölümünden paylaşabilirsiniz. Okuduğunuz için teşekkür ederim.

Github çalışma linki: https://github.com/fatihsoysal/WP-REST-API-and-PHP-WordPress-Insert-Post/

Yorumlar
İçeriği beğendiniz mi? Bir tartışma başlatın veya görüşlerinizi paylaşın.
Yorum Yaz

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

E-posta Bülteni
Yazılım Topluluğuna Katılın
En son güncellemeleri, yaratıcı ipuçlarını ve özel kaynakları doğrudan e-posta kutunuza alın. Tasarım ve inovasyonun geleceğini birlikte keşfedelim.