Skip to content

Instantly share code, notes, and snippets.

@AngeloR
Last active December 17, 2015 11:19
Show Gist options
  • Save AngeloR/5601095 to your computer and use it in GitHub Desktop.
Save AngeloR/5601095 to your computer and use it in GitHub Desktop.
Grab an OPML file generated by Fargo.io and return an JSON array of the outline
<?php
$OPML = 'ENTER THE LINK TO YOUR OPML FILE';
$rawOPML = file_get_contents($OPML);
$doc = new DOMDocument();
$doc->loadXML($rawOPML);
$arrayOutline = OPMLToArray($doc->childNodes->item(0));
$jsonOutline = json_encode($arrayOutline);
function OPMLToArray($node) {
$array = false;
if($node->hasAttributes()) {
foreach($node->attributes as $attr) {
$array['attributes'][$attr->nodeName] = $attr->nodeValue;
}
}
if($node->hasChildNodes()) {
if($node->childNodes->length == 1) {
if($node->firstChild->nodeName == '#text') {
$array = $node->firstChild->nodeValue;
}
else {
$array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
}
}
else {
foreach($node->childNodes as $childNode) {
if($childNode->nodeType != XML_TEXT_NODE) {
$array[$childNode->nodeName][] = OPMLToArray($childNode);
}
}
}
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment