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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?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/