For a Laravel project I needed to show latest three blog posts on the homepage. The blog was made with Wordpress and after some research I found a cool plugin called REST API that allows to get a json object of posts.
I made a Laravel Controller that uses curl to get the posts and send the response to jquery in the homepage, So as to build the widget dynamically.
The solution works well and I would like to know if there are parts to improve.
Php Class.
/**
* Developer: My name
* Date: 20/07/2016
* Time: 11:08
*/
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
class WpBlogController extends Controller
{
/*
* Using Wordpress Rest API
* http://v2.wp-api.org/
*
* Plugins to Install in Wordpress:
* 1. https://wordpress.org/plugins/rest-api/
* 2. https://wordpress.org/plugins/better-rest-api-featured-images/
* 3. https://wordpress.org/plugins/wp-rest-api-pure-taxonomies/
*/
const WP_USERNAME = "my_wp_username";
const WP_PASSWORD = "my_wp_password";
const WP_API_URL = "http://my.wp-blog.example/wp-json/wp/v2";
public function getLatestThreePosts(){
$curl = $this->getCurl();
//set the right url to get only three posts
curl_setopt($curl, CURLOPT_URL, self::WP_API_URL . "/posts?per_page=3&page=1");
$raw_posts = curl_exec($curl) or abort(502); //bad gateway error if request fails
//decode json and get the stdClass object
$raw_posts = json_decode($raw_posts);
$formatted_posts = [];
foreach($raw_posts as $post){
//some operations on the original html to get only the text
$curpost = [
"title" => trim(strip_tags($post->title->rendered)),
"content" => trim(strip_tags($post->content->rendered)),
"url" => $post->link,
"date" => Carbon::parse($post->date)->formatLocalized('%d %B %Y')
];
//abrreviate the post content.
$curpost["abbr"] = substr($curpost["content"],0,200).'...';
//get the featured image link if present
if (!is_null($post->better_featured_image)){
$curpost["thumb"] = $post->better_featured_image->media_details->sizes->medium->source_url;
}
//get the post categories
$cat_list = [];
foreach($post->pure_taxonomies->categories as $tax_obj){
$cat_list[] = $tax_obj->name;
}
$curpost["categories"] = $cat_list;
$formatted_posts[] = $curpost;
}
return response()->json($formatted_posts);
}
//returns a curl instance with http auth header already set
private function getCurl(){
// using http basic auth
$auth_header = ["Authorization: Basic " . base64_encode(self::WP_USERNAME . ":" . self::WP_PASSWORD)];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $auth_header
));
return $curl;
}
}
Jquery Code.
//load wp posts
function loadPosts(){
$.ajax({
url: '/api/blog/get-latest-three-posts',
method: 'GET',
dataType: "json",
success: function (posts, txtStatus, xhr) {
$.each(posts, function (index, post) {
//console.log(post);
var post_html = '<div class="post-block">';
if (post.hasOwnProperty('thumb')) {
post_html += '<div class="post-thumb">';
post_html += '<img src="' + post.thumb + '" alt="Post Thumb">';
post_html += '</div>';
}
post_html += '<div class="post-body">';
post_html += '<div class="post-categories">';
$.each(post.categories, function (index, category) {
post_html += '<span class="subcategory-label">' + category + '</span>';
});
post_html += '</div>';
post_html += '<h3><a href="' + post.url + '"target="_blank">' + post.title + '</a></h3>';
post_html += '<p class="post-date"><small>' + post.date + '</small></p>'
post_html += '<p>' + post.abbr + '</p>';
post_html += '<div class="text-left">'
post_html += '<a href="' + post.url + '" class="btn post-read-all" target="_blank">Leggi tutto</a>'
post_html += '</div>';
post_html += '</div>';
post_html += '</div>';
$('#wp-posts').append(post_html);
});
},
error: function (xhr) {
$('#wp-posts').append('<p><strong>Impossibile caricare i post in questo momento</strong></p>');
}
});
}