Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7a1805d2b1
Fetching contributors…

Cannot retrieve contributors at this time

file 95 lines (82 sloc) 2.176 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
<?php

/**
* DocRaptor
*
* @author Warren Krewenki
**/
class DocRaptor {

protected $api_key;
protected $document_content;
protected $document_url;
protected $document_type;
protected $name;
protected $test;

public function __construct($api_key=null){
if(!is_null($api_key)){
$this->api_key = $api_key;
}
$this->test = false;
$this->setDocumentType('pdf');
return true;
}

public function setAPIKey($api_key=null){
if(!is_null($api_key)){
$this->api_key = $api_key;
}
return $this;
}

public function setDocumentContent($document_content=null){
$this->document_content = $document_content;
return $this;
}

public function setDocumentUrl($document_url){
$this->document_url = $document_url;
return $this;
}

public function setDocumentType($document_type){
$document_type = strtolower($document_type);
$this->type = $document_type == 'pdf' || $document_type == 'xls' ? $document_type : 'pdf';
return $this;
}

public function setName($name){
$this->name = $name;
return $this;
}

public function setTest($test=false){
$this->test = (bool)$test;
return $this;
}

public function fetchDocument($filename = false){
if($this->api_key != ''){
$url = "https://docraptor.com/docs?user_credentials=".$this->api_key;
$fields = array(
'doc[document_type]'=>$this->type,
'doc[name]'=>$this->name,
'doc[test]'=>$this->test
);
if ( !empty($this->document_content) ){
$fields['doc[document_content]'] = urlencode($this->document_content);
} else {
$fields['doc[document_url]'] = urlencode($this->document_url);
}

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
if($result = curl_exec($ch)) {
if($filename){
file_put_contents($filename,$result);
}
} else {
echo 'error';
}
//close connection
curl_close($ch);
return $filename ? true : $result;
}

}
}

?>
Something went wrong with that request. Please try again.