Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
add xml feed support
Browse files Browse the repository at this point in the history
  • Loading branch information
mskian committed Jul 20, 2023
1 parent 957af87 commit 458fe1a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vercel
vendor
vendor
score.xml
45 changes: 45 additions & 0 deletions xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
header('X-Robots-Tag: noindex, nofollow', true);

// Convert JSON data to XML - https://craftytechie.com/how-to-convert-json-to-xml-in-php/

$JSON = file_get_contents('YOUR JSON API URL');
$JSON_arr = json_decode($JSON, true);

//print_r($JSON_arr);

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"></feed>');

function arraytoXML($json_arr, &$xml)
{
foreach($json_arr as $key => $value)
{
if(is_int($key))
{
$key = 'Element'.$key;
}
if(is_array($value))
{
$label = $xml->addChild($key);
arrayToXml($value, $label);
}
else
{
$xml->addChild($key, htmlspecialchars($value));
}
}
}

arraytoXML($JSON_arr, $xml);
print_r($xml->asXML('score.xml'));

?>

0 comments on commit 458fe1a

Please sign in to comment.