Last active
May 15, 2020 15:48
-
-
Save erfg12/727a50dc86ad17525d91cb92c45a4730 to your computer and use it in GitHub Desktop.
Convert Wordpress /feed/ XML to JSON and make CORS
This file contains hidden or 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
| // This uses jQuery! Get it at https://code.jquery.com | |
| $(document).ready(function () { | |
| $.ajax({ | |
| url: 'https://Website.com/wp-feed-xml-2-json.php', | |
| type: 'GET', | |
| dataType: "json", | |
| success: DisplayAllOps | |
| }); | |
| }); | |
| function DisplayAllOps(data) { | |
| //console.log(data); | |
| $.each(data.channel.item, function (i, item) { | |
| console.log(item.title); | |
| }); | |
| } |
This file contains hidden or 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 | |
| // This uses cURL! Be sure to have it enabled! Check with a PHPINFO script. | |
| header("Access-Control-Allow-Origin: *"); | |
| header("Content-Type: application/json; charset=UTF-8"); | |
| error_reporting(E_ALL); | |
| function GetURLData($url) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
| $data = curl_exec($ch); | |
| curl_close($ch); | |
| return $data; | |
| } | |
| function XMLtoJSON($url) { | |
| $fileContents = GetURLData($url); | |
| $xml = simplexml_load_string($fileContents); | |
| $json = json_encode($xml); | |
| return $json; | |
| } | |
| print_r(XMLtoJSON("https://WordPressSite.com/feed")); // home page | |
| print_r(XMLtoJSON("https://WordPressSite.com/search/QUERY_TEXT/feed")); // searching | |
| print_r(XMLtoJSON("https://WordPressSite.com/POST_TITLE/feed")); // post article | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment