Coder Social home page Coder Social logo

blogger's Introduction

Blogger API for Laravel

Unoffical Blogger (REST API) package for Laravel.

Features

  • Get blog information.
  • Retrieve posts.
  • Retrieve post by ID or path.
  • Search posts.
  • Retrieve pages.
  • Retrieve single page.

Installation

Add Blogger package to your Laravel application by running command:

composer require yugo/blogger

Configuration

If you are using Laravel 5.5 and upper, provider is automatically registered using auto package discovery from Laravel. If you get error to indicate provider not found, you can register new provider via config/app.php file.

/*
 * Package Service Providers...
 */
Yugo\Blogger\BloggerServiceProvider::class,

You can set up alias for Blogger package from the same config file.

// facade alias
'Blogger' => Yugo\Blogger\Facades\Blogger::class,

Before using Blogger facade, you must provide API key and full URL from your blog. API Key can be generated via this url.

Add two configs called key and url to file config/services.php.

'blogger' => [
    'key' => env('BLOGGER_KEY'),
    'url' => env('BLOGGER_URL', 'https://yourblog.blogspot.com'),
],

For security reason, you can define blogger.key and blogger.url config from .env file using sample below.

BLOGGER_KEY="secret-api-key"
BLOGGER_URL="https://yourblog.blogspot.com"

Usage

Before retrieving posts, pages, and comments, you must know your blog id first. To retrieve blog id data, you can use blog method from Blogger facade. For example:

$blog = Blogger::blog();

Available Methods

$blog = Blogger::blog();


// retrieving posts
$posts = Blogger::posts($blog['id']);
$posts = Blogger::search($keyword, $blogId);
$post = Blogger::postById($postId, $blogId);
$post = Blogger::postByPath($path, $blogId);

// retrieving pages
$pages = Blogger::pages($blog['id']);
$page = Blogger::page($pageId, $blogId);

If you want to include images in post, you can chain method withImages(bool $withImages = true), for example:

$posts = Blogger::withImages()->posts();

Best Practice

When you retrieving posts or pages, you must define blog id in every method. If you only have one blog and static blog id, you can follow this sample code for best practice.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use Yugo\Blogger\Facades\Blogger;

class BloggerController extends Controller
{
    /**
     * @var Blogger
     */
    private $blogger;

    public function __construct()
    {
        $blog = Cache::remember('blogger.blog', now()->addDays(7), function (){
           return Blogger::blog();
        });

        abort_if(empty($blog), 404, __('Blog not found.'));

        $this->blogger = Blogger::setBlog($blog['id']);
    }

    /**
     * @return JsonResponse
     */
    public function blog(): JsonResponse
    {
        return response()->json($this->blogger->blog());
    }

    /**
     * @return JsonResponse
     */
    public function posts(): JsonResponse
    {
        return response()->json($this->blogger->posts());
    }

    /**
     * @param string $id
     * @return JsonResponse
     */
    public function post(string $id): JsonResponse
    {
        return response()->json($this->blogger->postById($id));
    }

    /**
     * @param string $id
     * @return JsonResponse
     */
    public function comments(string $id): JsonResponse
    {
        return response()->json($this->blogger->comments($id));
    }

    /**
     * @param string $post
     * @param string $comment
     * @return JsonResponse
     */
    public function comment(string $post, string $comment): JsonResponse
    {
        return response()->json($this->blogger->comment($comment, $post));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function search(Request $request): JsonResponse
    {
        $this->validate($request, [
            'keyword' => ['required', 'string'],
        ]);

        return response()->json($this->blogger->search($request->keyword));
    }

    /**
     * @return JsonResponse
     */
    public function pages(): JsonResponse
    {
        return response()->json($this->blogger->pages());
    }

    /**
     * @param string $id
     * @return JsonResponse
     */
    public function page(string $id): JsonResponse
    {
        return response()->json($this->blogger->page($id));
    }
}

Next, you can define routes like this example in case you want access it directly via browser.

Route::get('blogger/blog', 'BloggerController@blog');
Route::get('blogger/posts', 'BloggerController@posts');
Route::get('blogger/post/{id}', 'BloggerController@post');
Route::get('blogger/post/{id}/comments', 'BloggerController@comments');
Route::get('blogger/post/{post}/comment/{comment}', 'BloggerController@comments');
Route::get('blogger/search', 'BloggerController@search');
Route::get('blogger/pages', 'BloggerController@pages');
Route::get('blogger/page/{id}', 'BloggerController@page');

Demo & Sample Code

Full running application can be found at blogger.aplikasi.live. You can look at the code from this repository.

License

MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.