Created
April 5, 2011 00:28
-
-
Save brendo/902776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* JSON to XML Converter | |
* | |
* @author Brent Burgoyne | |
* @link http://brentburgoyne.com | |
*/ | |
class Json_to_xml | |
{ | |
private static $dom; | |
public static function convert($json, $asXML = true) | |
{ | |
self::$dom = new DomDocument('1.0', 'utf-8'); | |
self::$dom->formatOutput = TRUE; | |
// remove callback functions from JSONP | |
if (preg_match('/(\{|\[).*(\}|\])/s', $json, $matches)) | |
{ | |
$json = $matches[0]; | |
} | |
else | |
{ | |
throw new JSONException("JSON not formatted correctly"); | |
} | |
$data = json_decode($json); | |
$data_element = self::_process($data, self::$dom->createElement('data')); | |
self::$dom->appendChild($data_element); | |
if($asXML) { | |
return self::$dom->saveXML(); | |
} | |
else { | |
return self::$dom->saveXML(self::$dom->documentElement); | |
} | |
} | |
private static function _process($data, $element) | |
{ | |
if (is_array($data)) | |
{ | |
foreach ($data as $item) | |
{ | |
$item_element = self::_process($item, self::$dom->createElement('item')); | |
$element->appendChild($item_element); | |
} | |
} | |
elseif (is_object($data)) | |
{ | |
$vars = get_object_vars($data); | |
foreach ($vars as $key => $value) | |
{ | |
$key = self::_valid_element_name($key); | |
$var_element = self::_process($value, self::$dom->createElement($key)); | |
$element->appendChild($var_element); | |
} | |
} | |
else | |
{ | |
$element->appendChild(self::$dom->createTextNode($data)); | |
} | |
return $element; | |
} | |
private static function _valid_element_name($name) | |
{ | |
$name = preg_replace('/^(.*?)(xml)([a-z])/i', '$3', $name); | |
$name = preg_replace('/[^a-z0-9_\-]/i', '', $name); | |
return $name; | |
} | |
} | |
class JSONException extends Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(TOOLKIT . '/class.event.php'); | |
require_once EXTENSIONS . '/xmlimporter/extension.driver.php'; | |
require_once EXTENSIONS . '/facebook_albums/extension.driver.php'; | |
require_once EXTENSIONS . '/facebook_albums/lib/class.json_to_xml.php'; | |
Class eventimport_facebook_albums extends Event{ | |
const ROOTELEMENT = 'import-facebook-albums'; | |
const ACCESS_TOKEN = ''; | |
public $eParamFILTERS = array( | |
); | |
public static function about(){ | |
return array( | |
'name' => 'Import Facebook Albums', | |
'author' => array( | |
'name' => 'Brendan Abbott', | |
'website' => 'http://brendan.sites.randb.com.au/', | |
'email' => 'brendan@randb.com.au'), | |
'version' => '1.0', | |
'release-date' => '2011-03-28T03:48:20+00:00', | |
'trigger-condition' => 'action[import-facebook-albums]'); | |
} | |
public static function getSource(){ | |
return '20'; | |
} | |
public static function allowEditorToParse(){ | |
return true; | |
} | |
public static function documentation(){ | |
return 'This is a custom event that imports Facebook albums from a Facebook page.'; | |
} | |
public function load(){ | |
return $this->__trigger(); | |
} | |
protected function __trigger(){ | |
$facebookURL = sprintf( | |
"https://graph.facebook.com/%s/albums/?fields=id,name,description,link,cover_photo,created_time,count&limit=1", | |
extension_facebook_albums::get('facebook-id'), self::ACCESS_TOKEN | |
); | |
// Fetch document: | |
$gateway = new Gateway(); | |
$gateway->init(); | |
$gateway->setopt('URL', $facebookURL); | |
$gateway->setopt('TIMEOUT', 5); | |
$gateway->setopt('CONTENTTYPE', 'application/json'); | |
$data = $gateway->exec(); | |
try { | |
$xml = Json_to_xml::convert($data); | |
} | |
catch (JSONException $ex) { | |
} | |
// Use Tidy to ensure that all crappy entities are removed (if only an exception was raised | |
// so this didn't have to happen every time :()) | |
if(class_exists('Tidy')) { | |
$tidy = new Tidy(); | |
$tidy->parseString( | |
$xml, array( | |
'input-xml' => true, | |
'numeric-entities' => true, | |
'output-xml' => true, | |
'wrap' => 0, | |
), 'utf8' | |
); | |
$xml = $tidy; | |
unset($tidy); | |
} | |
$importer = new EventImport_Facebook_Albums_Importer(Frontend::instance()); | |
$status = $importer->validate($xml->value, false); | |
if ($status == XMLImporter::__OK__) { | |
$importer->commit(); | |
} | |
$entries = $importer->getEntries(); | |
$xml_result = new XMLElement('import-facebook-albums'); | |
// Markup invalid: | |
if ($status == XMLImporter::__ERROR_PREPARING__) { | |
$xml_result->setAttribute('status', 'failed'); | |
$xml_result->setAttribute('error', $status); | |
foreach ($importer->getErrors() as $error) { | |
$xml_result->appendChild( | |
new XMLElement('error', $error) | |
); | |
} | |
} | |
// Invalid entry: | |
else if ($status == XMLImporter::__ERROR_VALIDATING__) { | |
$xml_result->setAttribute('status', 'failed'); | |
$xml_result->setAttribute('error', $status); | |
foreach ($entries as $entry) { | |
foreach($entry['errors'] as $error) { | |
$xml_result->appendChild( | |
new XMLElement('error', $error) | |
); | |
} | |
} | |
} | |
else { | |
$xml_result->setAttribute('status', 'success'); | |
$importer_result = array( | |
'created' => 0, | |
'updated' => 0, | |
'skipped' => 0 | |
); | |
foreach ($entries as $entry) { | |
$importer_result[$entry['entry']->get('importer_status')]++; | |
} | |
$result = new XMLElement('result', 'Import completed successfully'); | |
$result->setAttribute('created', $importer_result['created']); | |
$result->setAttribute('updated', $importer_result['updated']); | |
$result->setAttribute('skipped', $importer_result['skipped']); | |
$xml_result->appendChild($result); | |
} | |
return $xml_result; | |
} | |
} | |
class EventImport_Facebook_Albums_Importer extends XMLImporter { | |
public function options() { | |
$im = new XMLImporterManager(Frontend::instance()); | |
$importer = $im->create('facebookalbums'); | |
return $importer->options(); | |
} | |
} | |
class EventImport_Facebook_Albums_ImporterHelper extends XMLImporterHelpers { | |
public static function fetchAlbumCover($photo_id) { | |
$albumCover = sprintf( | |
"http://graph.facebook.com/%s/?fields=picture&limit=1", | |
$photo_id | |
); | |
// Fetch document: | |
$gateway = new Gateway(); | |
$gateway->init(); | |
$gateway->setopt('URL', $albumCover); | |
$gateway->setopt('TIMEOUT', 5); | |
$gateway->setopt('CONTENTTYPE', 'application/json'); | |
$data = $gateway->exec(); | |
try { | |
$photo = json_decode($data); | |
return $photo->picture; | |
} | |
catch (Exception $ex) { | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment