Skip to content

Instantly share code, notes, and snippets.

@scottgruber
Last active June 13, 2024 20:48
Show Gist options
  • Save scottgruber/d378b29436708afd2c7a73a5a041408c to your computer and use it in GitHub Desktop.
Save scottgruber/d378b29436708afd2c7a73a5a041408c to your computer and use it in GitHub Desktop.
PHP script to import a JSON data source into a Perch Runway content collection
{
"data": [
{
"id": 14832,
"Title": "First story title",
"Content": "postBody. Long body of markdown or html removed for this example",
"Date": "2017-01-13",
"Permalink": "first-story-title",
"author_given_name": "Scott",
"author_family_name": "Gruber"
},
{
"id": 15833,
"Title": "Second story title",
"Content": "postBody. Long body of markdown or html removed for this example",
"Date": "2017-01-13",
"Permalink": "second-story-title",
"author_given_name": "Scott",
"author_family_name": "Gruber"
}
]
}
<?php
// Create a new insthAPI(1.0, 'my_importer');
$API = new PerchAPI(1.0, 'my_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/article/post', '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 = "/path/to/your/data/file/data.json";
$json = file_get_contents($json_url);
echo $json;
$links = json_decode($json, true);
if (is_array($links)) {
foreach($links['data'] as $item) {
// Match perch content id names from a template to your data source
$Importer->add_item([
'heading' => $item['Title'],
'body' => $item['Content'],
'slug' => $item['Permalink'],
'date' => $item['Date'],
]);
}
}
else {
echo "This is some data, but doesn't look like an array";
}
}
catch (Exception $e) {
die('Error: '.$e->getMessage());
}
?>
<h2><perch:content id="heading" type="text" label="Heading" required="true" title="true" /></h2>
<p class="date"><perch:content id="date" type="date" label="Date" format="%d %B %Y" required="true" /></p>
<perch:content id="body" type="textarea" label="Body" html="true" editor="markitup" required="true" bucket="images" imageclasses="Pull Right|pull-left, pull-right" />
<p class="vcard">
&copy; <perch:content id="date" type="date" label="Date" format="Y" />
<span class="fn n">
<perch:content id="author_given_name" type="text" label="Author given name" />
<perch:content id="author_family_name" type="text" label="Author family name" />
</span>
</p>
<perch:content id="slug" for="heading" type="slug" suppress="true" label="Slug" />
<perch:after>
<perch:if exists="paging">
<div class="paging">
Page <perch:content id="current_page" type="hidden" /> of <perch:content id="number_of_pages" type="hidden" />
<perch:if exists="not_first_page">
<a href="<perch:content id="prev_url" type="hidden" encode="false" />">Previous</a>
</perch:if>
<perch:if exists="not_last_page">
<a href="<perch:content id="next_url" type="hidden" encode="false" />">Next</a>
</perch:if>
</div>
</perch:if>
</perch:after>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment