view goodreads/addToGRShelf.php @ 47:2a3943ced610

centralise db query even more
author Charlie Root
date Sun, 06 Jan 2019 12:17:05 -0500
parents 077b0a0a3e6d
children 385ddd7c4b55 a67bf725e87b
line wrap: on
line source

<?php
include "UTFUtil.php";

$userID=$_GET['userID'];
$listName=urldecode($_GET['listName']);
//echo $userID;
//echo $listName;

include "../../private/db.php";
$ynrList = "CALL b_getUserSavedList($userID,\"$listName\")";
//echo $ynrList;
//echo $add;
$results = mysqli_query($link, $ynrList) or exit( mysqli_error( $link ));

require_once('GoodreadsAPI.php');
session_start();
$access_token = $_SESSION['access_token'];

if($access_token == "")
{
	$back=$_GET['backLocation'];

	session_start();
	$_SESSION['backLocation'] = $back;

	$connection = new GoodreadsAPI(CONSUMER_KEY, CONSUMER_SECRET);
	$request_token = $connection->getRequestToken(CALLBACK_URL);

	$_SESSION['oauth_token']  = $request_token['oauth_token'];
	$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

	$authorize_url = $connection->getLoginURL($request_token);
	echo $authorize_url; 
}
else
{
	//print_r($access_token);
	$obj = new GoodreadsApi(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

	while($line = mysqli_fetch_assoc($results)) {
		$isbn = trim($line["ISBN"]);
		if($isbn != "")
		{
			$url= "http://www.goodreads.com/book/isbn?isbn=" . $isbn . "&key=8vxmKmAn8AVolobtvBbMIg";
			//echo $url;
			$crl = curl_init();
			$timeout = 5;
			curl_setopt ($crl, CURLOPT_URL,$url);
			curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
			curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
			$ret = curl_exec($crl);
			curl_close($crl);

			$ret = preg_replace("/[^[:alnum:][:punct:][:space:]]/","",$ret);
			if(trim($ret) != "")
			{
				$xml = new SimpleXMLElement(convert_utf8($ret));			
				$book_id = $xml->book->id;
				if(trim($book_id) != "")
				{
					$content = $obj->doPost('http://www.goodreads.com/shelf/add_to_shelf.xml', array("name" => "$listName", "book_id" => "$book_id"));
					//print_r($content);
					sleep(0.5);
				}
			}
		}
	}

	unset ($obj);

	echo trim($listName);
}
?>