I have a PHP page called bookscript.php dedicated to converting Goodreads API results from XML format to JSON format:
<?php
$xml_string = file_get_contents("https://www.goodreads.com/search?q=" .$_REQUEST['search']. "&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $json;
?>
And on my main page I plug bookscript.php into this AJAX call:
function GetBooks(request) {
//Replace spaces with a '+'
var url = request.term.replace(/\s/g,"+");
return $.ajax({
'url': "php/bookscript.php?search=" + url,
'dataType': 'json'
}).then(function(data){
return $.map(data.results || [], function(v,i){
return {
label: v.work.title + ' BOOK (' + v.work.original_publication_year + ')',
value: v.work.title
}
});
})
But this isn't working. When I visit http://www.example.com/php/bookscript.php?search=dog , all that displays on the page is the word "false".
Compare this to the actual results from Goodreads: https://www.goodreads.com/search/index.xml?&key=ZuqW9sL15d3JvEwmLyaNCg&q=dog
So my question is... what is going wrong with my method? I do have SSL working on my site (not sure if that is related at all) so that should rule that out as the issue....
I do know the GET Request code should be valid because that same code works with other APIs that DO use JSON format.
Edit: The problem actually turned out to be that I did not have PHP installed on my server. =)