Today I fixed the clunky front page. The latest blog post's excerpt is now gathered server-side instead of client-side. My solution uses PHP. I created a class for accessing Ghost's public API.

   This took longer than it should have because I initially misspelled the parameters of the GET request and rather than check those carefully, I assumed that my entire approach was wrong. The crazy notion, that oAuth was a good solution, was thrown my way in Ghost's slack channel, and so I distracted myself with Ghost's documentation on oAuth until I remembered how the internet worked. A server doesn't care whether the GET request is generated by another server or client-side code in a web browser. Both are clients as far as a server is concerned. GETting the data was the right solution. I took a close look at the keys of the parameters I had used in my first GET request, and found my error. I had used - in place of _, client-id instead of client_id.

   After slapping my forehead, I pulled together a simple solution: use php5-curl to send the GET request, generate the URL for the GET with a constructor function which has the same arguments as the AJAX solution published by the Ghost team.

class Ghost
  const USER = "ghost-frontend";
  const PASS = <enter your ghost-frontend password>;
  const URL_API = <enter the domain>."/ghost/api/v0.1/";

  public $response;

  public function __construct($endpoint, array $api_params = NULL)
     $params = array_merge(
                      'client_id' => self::USER,
                      'client_secret' => self::PASS
     $this->response = json_decode(curl_get(self::URL_API.$endpoint.'/', $params ));

   This is the PHP version of what the Ghost team's does.

   The three constants at the top of the Ghost class are for users to configure. Username, password, and domain.

   curl_get() uses cUrl for GETting. I think I found the function on Stack Exchange. Standard stuff. The default options don't return the response header.

function curl_get($url, array $get = NULL, array $options = array())
  $defaults = array(
    CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
  $ch = curl_init();
  curl_setopt_array($ch, ($options + $defaults));
  if( ! $result = curl_exec($ch))
  return $result;

   To use my Ghost Class, you instantiate it whenever you want to make a GET request. The constructor function accepts the arguments for the GET like so: $ghost = new Ghost('posts',array('limit'=>'1'));

   The Ghost API responds to a GET with JSON which we immediately decode into a PHP object as part of the Class we just instantiated. We dump this on the response property of the class. Thus to identify the data associated with the first post you would use: $ghost->response->posts[0].

   I was too lazy to bother with error checking the response to the GET request so this is not a robust implementation. I don't need it at this time. When I do, I'll add it.

Cover Image: Screenshot of my code in Sublime.