forked from krewenki/php-docraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocRaptor.class.php
112 lines (98 loc) · 2.56 KB
/
DocRaptor.class.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* DocRaptor
*
*
* @author Warren Krewenki
**/
class DocRaptor {
protected $api_key;
protected $document_content;
protected $document_url;
protected $document_type;
protected $name;
protected $test;
protected $url_protocol;
protected $base_url;
public function __construct($api_key=null){
if(!is_null($api_key)){
$this->api_key = $api_key;
}
$this->test = false;
$this->setDocumentType('pdf');
$this->setSecure(true);
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 setSecure($secure_url=false){
$this->url_protocol = $secure_url ? 'https' : 'http';
return $this;
}
public function setBaseUrl($base_url){
$this->base_url = $base_url;
return $this;
}
public function fetchDocument($filename = false){
if($this->api_key != ''){
$url = $this->url_protocol . '://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->base_url)){
$fields['doc[prince_options][base_url]'] = $this->base_url;
}
if ( !empty($this->document_content) ){
$fields['doc[document_content]'] = urlencode($this->document_content);
} else {
$fields['doc[document_url]'] = urlencode($this->document_url);
}
$fields_string = '';
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: ' . curl_error($ch));
}
//close connection
curl_close($ch);
return $filename ? true : $result;
}
}
}
?>