Last active
December 17, 2015 11:19
-
-
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
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 | |
$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