Bermain dengan Wordpress REST API v2
Judulnya Bermain, berarti bukan tutorial ya. Buat kamu2 yang mau eksperimen mengakses wordpress rest api dengan php curl, mungkin ini akan bermanfaat.
kemarin saya mencoba Worpress REST API yang katanya nge bug sebelumnya (tapi udah diperbaiki bungnya kok), pengen nyoba bikin aplikasi sendiri nge POST ke wordpress pakai curl.
<?php
$curl = curl_init();
curl_init_http($states);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$url = "http://my.ipaips.com/wp-json/wp/v2/posts/";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPGET, 0);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "arief:#######!@#");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'title'=>'FIRST POST DARI REST API',
'content' => 'Lorem Ipsum dolor rest api wordpress hello world!',
'status' => 'publish',));
$result = curl_exec($curl)
var_dump($result);
?>
di .htaccess
Selamat mencoba
ini scriptnya :
$curl = curl_init();
curl_init_http($states);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$url = "http://my.ipaips.com/wp-json/wp/v2/posts/";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPGET, 0);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "arief:#######!@#");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'title'=>'FIRST POST DARI REST API',
'content' => 'Lorem Ipsum dolor rest api wordpress hello world!',
'status' => 'publish',));
$result = curl_exec($curl)
var_dump($result);
?>
kalau hasilnya seperti ini :
{
"code": "rest_cannot_create",
"message": "Sorry, you are not allowed to create posts as this user.",
"data": {
"status": 401
}
}
berarti kamu harus menambahkan
{
"code": "rest_cannot_create",
"message": "Sorry, you are not allowed to create posts as this user.",
"data": {
"status": 401
}
}
berarti kamu harus menambahkan
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
di .htaccess
Selamat mencoba

Komentar
Posting Komentar