Skip to content

Instantly share code, notes, and snippets.

@scottgruber
Last active December 5, 2023 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottgruber/bf45065be4191be679c852c5a7d3defe to your computer and use it in GitHub Desktop.
Save scottgruber/bf45065be4191be679c852c5a7d3defe to your computer and use it in GitHub Desktop.
Two scripts to export Perch blog posts to JSON and then import the JSON into a Perch Runway collection
<?php
//include('../perch/runtime.php');
$site_url = 'https://'.$_SERVER['HTTP_HOST'];
$items = perch_blog_custom([
'count' => 100,
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'skip-template' => true,
]);
//print_r($items);
if (count($items)) {
foreach($items as $item) {
$itemDynamicFields = json_decode($item['postDynamicFields'], true);
// print_r($itemDynamicFields);
// get objects of postDynamicFields
//$imageAsset = $itemDynamicFields -> image -> assetID;
//$excerptRaw = $itemDynamicFields -> excerpt -> raw;
if (isset($itemDynamicFields['image']['assetID'])) {
$imageAsset = $itemDynamicFields['image']['assetID'];
};
if (isset($itemDynamicFields['excerpt']['raw'])){
$excerptRaw = $itemDynamicFields['excerpt']['raw'];
};
$feed['data'][] = [
'id' => $item['postID'],
'title' => $item['postTitle'],
'raw_excerpt' => $excerptRaw,
'processed_excerpt' => $item['excerpt'],
'raw_html' => $item['postDescRaw'],
'processed_html' => $item['postDescHTML'],
'category' => $item['category'],
'slug' => $item['postSlug'],
'date_published' => date('c', strtotime($item['postDateTime'])),
'publish' => '0'
//'url' => $site_url.'/blog/post.php?s='.$item['postSlug'],
//'image' => $item['image'],
//'image_assetID' => $imageAsset,
//'dynamicfields' => $item['postDynamicFields']
];
}
}
header('Content-Type: application/json');
echo json_encode($feed);
<?php
if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');
// Create a new insthAPI(1.0, 'my_importer');
$API = new PerchAPI(1.0, 'articles_importer');
$Importer = $API->get('CollectionImporter');
// Tell the importer which collection to import into.
$Importer->set_collection('Articles');
// The importer uses a template to know how to process the incoming content. For example, you might have a field called `publish_on`, but the importer only knows that the content should be processed as a date because it can look at the definition of `id="publish_on"` in the supplied template.
$Template = $API->get('Template');
// PerchAPI_Template::set()` takes two arguments:
// 1. The path to the template and
// 2. The tag namespace to use
$Template->set('content/articles/article', 'content');
$Importer->set_template($Template);
// You're now ready to start importing content. If your collection is new and you want to clean out previous test imports, you can call `empty_collect()`. Don't do that on a collection with valuable content, it's a hard delete.
// but here it is ready to be uncommented to clean up tests or play with
// $Importer->empty_collection();
// At this point you want to loop through your data source (whatever that may be) and map your content to IDs from your collection template. For each collection item you want to add, call `add_item()` with an associative array where the _keys_ are the IDs from your template, and the _values_ are the content you want to assign to that field.
// The `add_item()` method validates required fields and will throw an exception if any item of content cannot be imported. For this reason, it's best to use a try/catch block:
try {
$json_url = "blog.json";
//echo $json_url;
$json = file_get_contents($json_url, true);
$items = json_decode($json, true);
if (is_array($items)) {
foreach($items['data'] as $item) {
// Match perch content id names from a template to your data source
$Importer->add_item([
'heading' => $item['title'],
'standfirst' => $item['raw_excerpt'],
'body' => $item['raw_html'],
'slug' => $item['slug'],
'tags' => $item['category'],
'date' => $item['date_published'],
'publish' => $item['publish'],
]);
}
}
else {
echo "There is some data, but this doesn't look like an array.";
}
}
catch (Exception $e) {
die('Error: '.$e->getMessage());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment