2

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. =)

marinarasauce67
  • 123
  • 1
  • 6
  • 1
    `simplexml_load_string` returns a SimpleXMLElement object. From the documentation I don't see it as json serializable : https://secure.php.net/manual/fr/class.simplexmlelement.php You probably have to create an array from it. – Robert Vionny Apr 05 '16 at 06:36
  • 1
    try data.search.results || [] instead of data.results – AlmasK89 Apr 05 '16 at 06:58

2 Answers2

1

json_encode expects an array or object with public properties as input. If it fails, it returns a boolean false. simplexml_load_string returns a SimpleXMLElement.

You need to loop over the SimpleXMLElement and create an array from it or look for a function or class that does it for you. You can find plenty of implementations on the internet.

See this SO answer: https://stackoverflow.com/a/20431742/1479962

Community
  • 1
  • 1
ar34z
  • 2,609
  • 2
  • 24
  • 37
1

I've tested with this code, and everything is fine,

<?php $xml_string = file_get_contents("https://www.goodreads.com/search?q=dog&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);?>

<pre>
    <?php print_r($array); ?>
</pre> 

Array printed like:

Array
(
    [Request] => Array
        (
            [authentication] => true
            [key] => Array
                (
                )

            [method] => Array
                (
                )

        )

    [search] => Array
        (
            [query] => Array
                (
                )

            [results-start] => 1
            [results-end] => 20
            [total-results] => 26080
            [source] => Goodreads
            [query-time-seconds] => 0.31
            [results] => Array
                (
                    [work] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 10479953
                                    [books_count] => 38...

Maybe error on your client side? What is result of ajax call?

AlmasK89
  • 1,332
  • 7
  • 16
  • This is the strangest thing. I have now discovered that it works (and very well!) in my localhost, but that it doesn't work on my real server (the code is exactly the same between the local files and the server files). Do you have any guesses as to what might be the cause of that? Thank you. – marinarasauce67 Apr 05 '16 at 08:02
  • 1
    first, try to manually check, if php script works. Like in my "example". Then, you can see is it on server side, or another mistake. If php script doesn't work, then check version of php, maybe simplexml is turned off http://php.net/manual/en/simplexml.installation.php – AlmasK89 Apr 05 '16 at 08:12
  • 1
    Friend, thank you so much. Your solutions have fixed my problem and it's working fantastically now. Thanks again. – marinarasauce67 Apr 05 '16 at 09:09
  • 1
    You are welcome) If you can, please write about your problem and how you fix it. What was that? Maybe, will be helpful for future viewers. – AlmasK89 Apr 05 '16 at 09:23