Skip to content

Instantly share code, notes, and snippets.

@tedliou
Created March 6, 2018 01:07
Show Gist options
  • Save tedliou/65173ee8c3c68bb0d0b045ec1cffa8a7 to your computer and use it in GitHub Desktop.
Save tedliou/65173ee8c3c68bb0d0b045ec1cffa8a7 to your computer and use it in GitHub Desktop.
Wordpress RSS To JSON
<?php
$target_feed = 'http://' . $_SERVER['HTTP_HOST'] . '/feed';
//echo $target_feed;
$target_array = (array) simplexml_load_file($target_feed);
$temp_array = ((array) $target_array['channel'])['item'];
$output_array = array();
foreach ($temp_array as $data) {
$data = (array) $data;
if (!isset($data['enclosure'])){
$media = null;
}
else{
$media = ((array)$data['enclosure'])['@attributes']['url'];
}
$convert_to_source = array(
'id' => (int)$data['post-id'],
'date' => $data['pubDate'],
'title' => $data['title'],
'link' => $data['link'],
'media' => $media
);
array_push($output_array, $convert_to_source);
}
echo json_encode($output_array, JSON_UNESCAPED_SLASHES);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment