Solved

goodreads.com php

Posted on 2012-03-29
5
920 Views
Last Modified: 2012-03-30
Hey guys,

I was wondering if anyone knew any sites or tutorials that would assist me in getting back book reviews from http://www.goodreads.com api.  There are no php samples to give me a head start.  Has anyone used the api from goodreads?

thanks in advance
0
Comment
Question by:MK15
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Derokorian earned 500 total points
ID: 37782728
I would look at the cURL library for PHP -> http://www.php.net/manual/en/book.curl.php
Then the URL you send a request to with cURL would be built using the different API Methods listed here http://www.goodreads.com/api See my example below:

<?php

$GoodReads_API_KEY = 'Your_Key'; // Replace with your key

$bookurl = 'http://www.goodreads.com/api/book_url/Ender%27s%2BGame?key='.$GoodReads_API_KEY; // specific url to request a book, this can be build dynamically by inserting different methods where it says book_url and such. See the API docs to see more about how the different URLs should be look

$ch = curl_init(); // Initialize curl  handle
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5 second timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the result on success, rather than just TRUE
curl_setopt($ch, CURLOPT_URL, $bookurl); // Set the url for curl to request to

$response = curl_exec($ch); // makes the request and gets the response

// Process $response as xml 
$xml = new SimpleXML($response);

Open in new window

0
 

Author Comment

by:MK15
ID: 37784853
hi,
let me start of by thanking you for your help.

I have managed to retrieve the data without creating an xml object, however there is a lot of data which i wish to remove. i want only the iframe enclosing the review and nothing else, how would you suggest i tackle this issue.


$bookurl = 'http://www.goodreads.com/book/isbn?isbn=0441172717&key='.$GoodReads_API_KEY;
// specific url to request a book, this can be build dynamically by inserting different methods where it says book_url and such. See the API docs to see more about how the different URLs should be look

$ch = curl_init(); // Initialize curl  handle
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5 second timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the result on success, rather than just TRUE
curl_setopt($ch, CURLOPT_URL, $bookurl); // Set the url for curl to request to

$response = curl_exec($ch); // makes the request and gets the response

// Process $response as xml 
$xml = new SimpleXMLElement($response);
echo '<pre>';
print_r($response);
echo '</pre>'

Open in new window


result

  
    true
      
    
  
  
  53732
  
  
  
  
  http://www.goodreads.com/images/nocover-111x148.jpg
  http://www.goodreads.com/images/nocover-60x80.jpg
  1990
  9
  1
  Ace/Berkley Books
  eng
  false
  
  
  234225
  137
  3634639
  book
  
  
  
  1965
  Dune
  5:79751|4:52480|3:32518|2:11728|1:9303|total:185780
  186613
  742519
  252551
  4399


  
  
  
  
  
  
  
  
  
  
    


    
    58
        
    
    
    
    3.88
    293472
    7134
  
  
    
    
      
  #goodreads-widget {
    font-family: georgia, serif;
    padding: 18px 0;
    width:565px;
  }
  #goodreads-widget h1 {
    font-weight:normal;
    font-size: 16px;
    border-bottom: 1px solid #BBB596;
    margin-bottom: 0;
  }
  #goodreads-widget a {
    text-decoration: none;
    color:#660;
  }
  iframe{
    background-color: #fff;
  }
  #goodreads-widget a:hover { text-decoration: underline; }
  #goodreads-widget a:active {
    color:#660;
  }
  #gr_footer {
    width: 100%;
    border-top: 1px solid #BBB596;
    text-align: right;
  }
  #goodreads-widget .gr_branding{
    color: #382110;
    font-size: 11px;
    text-decoration: none;
    font-family: verdana, arial, helvetica, sans-serif;
  }


  
Dune Reviews

//---------------------------------------------------------------------------------------------------------------------there is an iframe here which encloses the reviews - i need to have this iframe and nothing else.
  
  

    Reviews from Goodreads.com
  



      ]]>
    
  
  
          
          
          
          
          
          
          
          
          
          
      
  
    
  8
  
  http://www.goodreads.com/book_link/follow/8?book_id=53732

  1027
  
  http://www.goodreads.com/book_link/follow/1027?book_id=53732

  9
  
  http://www.goodreads.com/book_link/follow/9?book_id=53732

  4
  
  http://www.goodreads.com/book_link/follow/4?book_id=53732

  2
  
  http://www.goodreads.com/book_link/follow/2?book_id=53732

  10
  
  http://www.goodreads.com/book_link/follow/10?book_id=53732

  5
  
  http://www.goodreads.com/book_link/follow/5?book_id=53732

  2102
  
  http://www.goodreads.com/book_link/follow/2102?book_id=53732

  1602
  
  http://www.goodreads.com/book_link/follow/1602?book_id=53732

  107
  
  http://www.goodreads.com/book_link/follow/107?book_id=53732

  7
  
  http://www.goodreads.com/book_link/follow/7?book_id=53732

  1
  
  http://www.goodreads.com/book_link/follow/1?book_id=53732

  
  
    
166801
1

45935


two-volume omnibus series.
    
    See also:
    * Prelude to Dune
    * Legends of Dune
]]>




17
8
true

Open in new window


many thanks for your help, it is very much appreciated.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 37786527
Isnt this the same question framed in a different way.
I have provided a solution in that question .
Please try that.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 37787180
$bookurl = 'http://www.goodreads.com/book/isbn?isbn=0441172717&key='.$GoodReads_API_KEY;

Open in new window

Notice the URL you used doesn't make a request for the API but rather goes to the site.
0
 

Author Closing Comment

by:MK15
ID: 37789686
thanks for your help mate
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question