Solved

goodreads.com php

Posted on 2012-03-29
5
887 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
thanks for your help mate
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now