$scribd_api_key = "2ssrjo6wb8pdmsvclww09";
$scribd_secret = "sec-1wz1qwludu9b6653y1bvnlamj5";
$scribd = new scribd($scribd_api_key, $scribd_secret);
$data = $scribd->getList(); // returns
?>
/**
* Class taken from Scribd API website. Originally written by Robert Pottorff.
*
* Modification / Fixes by Gareth Moss
*
* - Generation of API Signature was not working. Was being passed wrong params
* and didn't include api_key in params. Also included file param when documentation
* states that it shouldn't.
*
* - upload() function didn't include rev_id
*
* - changeSettings() didn't support all the params offered by api. Params are now
* passed as an array.
*
* - postRequest() changed to return either results or true|false
*
*/
class Scribd {
public $api_key;
public $secret;
private $url;
public $session_key;
public $my_user_id;
private $error;
public function __construct($api_key, $secret) {
$this->api_key = $api_key;
$this->secret = $secret;
$this->url = "http://api.scribd.com/api?api_key=" . $api_key;
}
/**
* Upload a document from a file
* @param string $file : relative path to file
* @param string $doc_type : PDF, DOC, TXT, PPT, etc.
* @param string $access : public or private. Default is Public.
* @param int $rev_id : id of file to modify
* @return array containing doc_id, access_key, and secret_password if nessesary.
*/
public function upload($file, $doc_type = null, $access = null, $rev_id = null){
$method = "docs.upload";
$params['doc_type'] = $doc_type;
$params['access'] = $access;
$params['file'] = "@".$file;
$params['rev_id'] = $rev_id;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Upload a document from a Url
* @param string $url : absolute URL of file
* @param string $doc_type : PDF, DOC, TXT, PPT, etc.
* @param string $access : public or private. Default is Public.
* @return array containing doc_id, access_key, and secret_password if nessesary.
*/
public function uploadFromUrl($url, $doc_type = null, $access = null, $rev_id = null){
$method = "docs.uploadFromUrl";
$params['url'] = $url;
$params['access'] = $access;
$params['rev_id'] = $rev_id;
$params['doc_type'] = $doc_type;
$data_array = $this->postRequest($method, $params);
return $data_array;
}
/**
* Get a list of the current users files
* @return array containing doc_id, title, description, access_key, and conversion_status for all documents
*/
public function getList(){
$method = "docs.getList";
$result = $this->postRequest($method, $params);
return $result['resultset'];
}
/**
* Get the current conversion status of a document
* @param int $doc_id : document id
* @return string containing DISPLAYABLE", "DONE", "ERROR", or "PROCESSING" for the current document.
*/
public function getConversionStatus($doc_id){
$method = "docs.getConversionStatus";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result['conversion_status'];
}
/**
* Get settings of a document
* @return array containing doc_id, title , description , access, tags, show_ads, license, access_key, secret_password
*/
public function getSettings($doc_id){
$method = "docs.getSettings";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Change settings of a document
* @param array $doc_ids : document id, comma sep
* @param array $params :
* title
* description
* access ("public", "private")
* licence ("by", "by-nc", "by-nc-nd", "by-nc-sa", "by-nd", "by-sa", "c", "pd")
* show_adds ("true", "false")
* link_back_url
* tags (csv)
* author
* publisher
* when_published
* edition
* @return boolean
*/
public function changeSettings($doc_ids, $params = array())
{
$method = "docs.changeSettings";
$params['doc_ids'] = $doc_ids;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Delete a document
* @param int $doc_id : document id
* @return 1 on success;
*/
public function delete($doc_id){
$method = "docs.delete";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Search the Scribd database
* @param string $query : search query
* @param int $num_results : number of results to return (10 default, 1000 max)
* @param int $num_start : number to start from
* @param string $scope : scope of search, "all" or "user"
* @return array of results, each of which contain doc_id, secret password, access_key, title, and description
*/
public function search($query, $num_results = null, $num_start = null, $scope = null){
$method = "docs.search";
$params['query'] = $query;
$params['num_results'] = $num_results;
$params['num_start'] = $num_start;
$params['scope'] = $scope;
$result = $this->postRequest($method, $params);
return $result['result_set'];
}
/**
* Login as a user
* @param string $username : username of user to log in
* @param string $password : password of user to log in
* @return array containing session_key, name, username, and user_id of the user;
*/
public function login($username, $password){
$method = "user.login";
$params['username'] = $username;
$params['password'] = $password;
$result = $this->postRequest($method, $params);
$this->session_key = $result['session_key'];
return $result;
}
/**
* Sign up a new user
* @param string $username : username of user to create
* @param string $password : password of user to create
* @param string $email : email address of user
* @param string $name : name of user
* @return array containing session_key, name, username, and user_id of the user;
*/
public function signup($username, $password, $email, $name = null){
$method = "user.signup";
$params['username'] = $username;
$params['password'] = $password;
$params['name'] = $name;
$params['email'] = $email;
$result = $this->postRequest($method, $params);
return $result;
}
private function postRequest($method, $params){
$params['method'] = $method;
$params['session_key'] = $this->session_key;
$params['my_user_id'] = $this->my_user_id;
$params['api_key'] = $this->api_key;
$post_params = array();
foreach ($params as $key => &$val) {
if(!empty($val)){
if (is_array($val)) $val = implode(',', $val);
if($key != 'file' && substr($val, 0, 1) == "@"){
$val = chr(32).$val;
}
$post_params[$key] = $val;
}
}
$post_params['api_sig'] = $this->generate_sig($post_params, $this->secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params );
$xml = curl_exec( $ch );
$result = simplexml_load_string($xml);
curl_close($ch);
if($result['stat'] == 'fail')
{
//This is ineffecient.
$error_array = (array)$result;
$error_array = (array)$error_array;
$error_array = (array)$error_array['error'];
$error_array = $error_array['@attributes'];
$this->error = $error_array['code'];
throw new Exception($error_array['message'], $error_array['code']);
return false;
}
if($result['stat'] == "ok")
{
// if no array to return returns a string with a line break,
// so we have to check that
$result = $this->convert_simplexml_to_array($result);
if (strpos((string)$result, "\n") !== false)
{
return true;
}
return $result;
}
}
public static function generate_sig($params_array, $secret) {
$str = '';
ksort($params_array);
// Note: make sure that the signature parameter is not already included in
// $params_array. "file" should not be used in sig
foreach ($params_array as $k=>$v) {
if ($k == "file")
continue;
$str .= $k . $v;
}
$str = $secret . $str;
return md5($str);
}
public static function convert_simplexml_to_array($sxml) {
$arr = array();
if ($sxml) {
foreach ($sxml as $k => $v) {
if(isset($arr[$k])){
$arr[$k." ".(count($arr) + 1)] = self::convert_simplexml_to_array($v);
}else{
$arr[$k] = self::convert_simplexml_to_array($v);
}
}
}
if (sizeof($arr) > 0)
{
return $arr;
}
else
{
return (string)$sxml;
}
}
}
?>
echo ("
- ");
$chk_number = 9;
if(count($data)>0) {
foreach(array_reverse($data, true) as $data){
if ($chk_number % 9 == 0) { echo ("
- "); }
echo(""); echo (""); $chk_number++; if ($chk_number % 9 == 0) { echo (""); echo (""); echo (""); echo (""); }
}
}
echo("
".$data[page_count]." pages