Skip to content

Instantly share code, notes, and snippets.

@dansleboby
Last active July 31, 2017 05:23
Show Gist options
  • Save dansleboby/330d268e7c1d7a5516dbac738265e4d7 to your computer and use it in GitHub Desktop.
Save dansleboby/330d268e7c1d7a5516dbac738265e4d7 to your computer and use it in GitHub Desktop.
API to retrive, rename and delete docs fom camsacnner.net
<?php
class CamSacnnerApiHttpError extends Exception {}
class CamSacnnerApiBadLogin extends Exception {}
class CamSacnnerApiError extends Exception {}
class CamSacnnerApi {
private $baseUrl = "https://www.camscanner.com";
private $baseUrlJpg = null;
private $username = null;
private $password = null;
private $isLogged = false;
private $cookies = [];
private function _isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
private function _makeRequest($method, $url, $data = null, $headers = []) {
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->baseUrl.$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
if($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
if(!empty($data))
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
$result = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$cookies = curl_getinfo($ch, CURLINFO_COOKIELIST);
$errno = curl_errno($ch);
foreach($cookies as $cookie) {
$data = explode("\t", $cookie);
$this->cookies[$data[5]] = $data[6];
}
//close connection
curl_close($ch);
if(substr($responseCode, 0, 1) <> 2) {
throw new CamSacnnerApiHttpError("Error http call response: $responseCode to $url");
}
if($this->_isJson($result))
$result = json_decode($result);
return $result;
}
private function _isLogged() {
if($this->isLogged == false) {
throw new CamSacnnerApiError('Must be logged');
}
}
public function __construct($username = null, $password = null) {
if(is_null($username)) $username = $this->username;
if(is_null($password)) $password = $this->password;
$act = 'submit';
$login = $this->_makeRequest('POST', '/user/login', compact('username', 'password', 'act'));
if(!is_object($login)) {
throw new CamSacnnerApiBadLogin();
}
$this->isLogged = true;
}
public function docs() {
$this->_isLogged();
$docs = $this->_makeRequest('POST', '/doc/list', null, ['x-requested-with: XMLHttpRequest']);
if(!is_object($docs) && isset($docs->data->list)) {
throw new CamSacnnerApiError();
}
$this->baseUrlJpg = str_replace('/u/CamScanner_Page/', '', $docs->data->server);
return $docs->data->list;
}
public function docDownloadPDF($docId) {
$this->_isLogged();
$result = $this->_makeRequest('POST', '/doc/downloadpdf', ['json_download' => json_encode(['docs' => [$docId.'.jdoc']])],['x-requested-with: XMLHttpRequest']);
if(isset($result->errno) || !isset($result->data)) {
throw new CamSacnnerApiError("Invalid doc id!");
}
$docs = $this->docs();
$mapDocs = [];
foreach($docs as $doc) $mapDocs[$doc->doc_id] = $doc->title;
return $result->data."?".http_build_query(['title' => $mapDocs[$docId].'.pdf']);
}
public function docDelete($doc_id) {
$this->_isLogged();
$result = $this->_makeRequest('POST', '/doc/delete', compact('doc_id'),['x-requested-with: XMLHttpRequest']);
if(isset($result->errno) || !isset($result->data)) {
throw new CamSacnnerApiError("Invalid doc id!");
}
return $result->data;
}
public function docUpdate($doc_id, $title) {
$this->_isLogged();
$result = $this->_makeRequest('POST', '/doc/update', compact('doc_id', 'title'), ['x-requested-with: XMLHttpRequest']);
if(isset($result->errno) || !isset($result->data)) {
throw new CamSacnnerApiError("Invalid doc id!");
}
return $result->data;
}
}
@dansleboby
Copy link
Author

TODO
Manage folder
Download jpg
Move file
Tags

@dansleboby
Copy link
Author

//EXEMPLE of usage

$CS = new CamSacnnerApi('USERNAME', 'PASSWORD');

//Retrives all docs
$CS->docs();

//Download a doc (doc_id from docs())
$CS->docDownloadPDF($doc_id);

//Delete doc
$CS->docDelete($doc_id);

//Rename doc
$CS->docUpdate($doc_id, 'NEW NAME');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment