Accessing Content API with PHP

And

  • How was Ghost installed and configured? Ghost Pro
  • What Node version, database, OS & browser are you using? Ghost Pro
  • What errors or information do you see in the console? No errors
  • What steps could someone else take to reproduce the issue you’re having? See the URL above

Our main site is on a server different from our Ghost blog. I am trying to access the Content API from our main site using PHP. I have successfully built a page on our main site using the Content API and the JavaScript Content API Client (see here: http://test.stoutbowman.com/blog-index-api.html), but am having a hard time figuring out how to do the same using PHP. I’m not a PHP expert: I can find and modify examples and get things to work, but that’s about it.

In my PHP example, I am replacing the code inserted by the JavaScript client with:

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://stout-bowman-associates- 
llc.ghost.io/ghost/api/v3/content/tags/?key=73af8261b1543c9a9446538e98&fields=name,slug');
$result = curl_exec($curl);
print_r($result);
curl_close($curl);

?>

You can see the result on the test PHP page. When I place this reponse from the Content API into an online JSON validator, it complains that it is not valid JSON, so that may be why I can’t get the PHP JSON functions to operate on the response. I need to extract the name of the tag and the slug in order to build the list of tags on the page.

Also, if you look at the List of Posts section, you’ll see that, while I request the post with:

curl_setopt($curl, CURLOPT_URL, 'https://stout-bowman-associates-llc.ghost.io/ghost/api/v3/content/posts/?key=73af8261b1543c9a9446538e98&fields=title,published_at,plaintext,url');

I am not getting the plaintext for the posts. Has anyone used PHP to retrieve this data from the Content API and can show me an example I can use? Thanks in advance.

Your result will be empty since the default behavior of curl is to print to stdout. Try this:

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://stout-bowman-associates-llc.ghost.io/ghost/api/v3/content/tags/?key=73af8261b1543c9a9446538e98&fields=name,slug');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // This was missing!
$result = json_decode(curl_exec($curl)); // You need to use json_decode here since curl returns a string
curl_close($curl);

foreach($result->tags as $tag) {
        print_r($tag);
}
?>

For the list posts call, you need to include the formats parameter:

https://stout-bowman-associates-llc.ghost.io/ghost/api/v3/content/posts/?key=73af8261b1543c9a9446538e98&formats=plaintext&fields=title,published_at,plaintext,url

Thanks for the help! After much experimentation, I got it working for both tags and posts. Should anyone else need this, here’s my implementation:

Within the div for the tags:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://stout-bowman-associates- 
llc.ghost.io/ghost/api/v3/content/tags/?key=[key]&limit=all&fields=name,slug');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl));
curl_close($curl);
echo '<ul class="tag-menu">';
foreach($result->tags as $tag) {
    echo '<li class="tag"><a class="brand-bg button" href="https://stout-bowman-associates-llc.ghost.io/tag/' . $tag->slug . '/">' . $tag->name . '</a></li>';
}
echo '</ul>';
?>

Within the div tag for the list of posts:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://stout-bowman-associates-llc.ghost.io/ghost/api/v3/content/posts/?key=[key]&limit=all&fields=title,published_at,custom_excerpt,url');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl));
curl_close($curl);

foreach($result->posts as $post) {
    echo '<p class="blog-items"><a href="' . $post->url . '"><strong>' . $post->title . '</strong></a><br />';
    $tdate = $post->published_at;
    $pubdate = date_create($tdate);
    echo '<span class="postDate">' . date_format($pubdate,"F j, Y") . '</span><br />';
    echo $post->custom_excerpt;
    echo '</p>';
}
?>

You can view the result at https://www.stoutbowman.com/blog-index.php