forked from cloudconvert/cloudconvert-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudConvert.class.php
191 lines (162 loc) · 5.22 KB
/
CloudConvert.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/*
* CloudConvert API Example Class
* 2013 by Lunaweb Ltd.
* Feel free to use, modify or publish it.
*/
class CloudConvert {
/*
* Maximum conversion timeout.
* This should be adjusted based on your PHP configuraion (max_execution_time etc)
* Only necesarry for server side conversions
*/
private $apikey;
private $url;
private $data;
private $options = array();
/*
* creates new Process ID.
* see: https://cloudconvert.org/page/api#start
*
*/
public static function createProcess($inputformat, $outputformat, $apikey = null) {
$instance = new self();
$instance -> apikey = $apikey;
$data = $instance -> req('https://api.cloudconvert.org/process', array(
'inputformat' => $inputformat,
'outputformat' => $outputformat,
'apikey' => $apikey
));
if (strpos($data -> url, 'http') === false)
$data -> url = "https:" . $data -> url;
$instance -> url = $data -> url;
return $instance;
}
/*
* uses existing Process by given Process URL
*/
public static function useProcess($url) {
$instance = new self();
if (strpos($url, 'http') === false)
$url = "https:" . $url;
$instance -> url = $url;
return $instance;
}
/*
* Set conversion option.
* examples:
* $converter -> setOption('email' => '1');
* $converter -> setOption('options[audio_bitrate]' => '128');
*
*/
public function setOption($name, $val) {
$this -> options[$name] = $val;
}
/*
* Uploads the input file (server side)
*/
public function upload($filepath, $outputformat) {
$this -> req($this -> url, array_merge(array(
'input' => 'upload',
'format' => $outputformat,
'file' => (class_exists('CURLFile') ? new CURLFile($filepath) : '@' . $filepath) // CURLFile is available PHP >= 5.5.0
)), $this -> options);
}
/*
* Let CloudConvert download the input file by a given URL and filename
*/
public function uploadByURL($url, $filename, $outputformat) {
$this -> req($this -> url, array_merge(array(
'input' => 'download',
'format' => $outputformat,
'filename' => $filename,
'link' => $url,
)), $this -> options);
}
/*
* returns Process URL
*/
public function getURL() {
return $this -> url;
}
/*
* Checks the current status of the process
*/
public function status($action = null) {
if (empty($this -> url))
throw new Exception("No process URL found! (Conversion not started)");
$this -> data = $this -> req($this -> url . ($action ? '/' . $action : ''));
return $this -> data;
}
public function cancel() {
return $this -> status('cancel');
}
public function delete() {
return $this -> status('delete');
}
/*
* Blocks until the conversion is finished
*/
public function waitForConversion($timeout = 120) {
$time = 0;
/*
* Check the status every second, up to timeout
*/
while ($time <= $timeout) {
sleep(1);
$time++;
$data = $this -> status();
if ($data -> step == 'error') {
throw new Exception($data -> message);
return false;
} elseif ($data -> step == 'finished' && isset($data -> output) && isset($data -> output -> url)) {
return true;
}
}
throw new Exception('Timeout');
return false;
}
/*
* Download output file to local target
*/
public function download($target) {
if (empty($this -> data -> output -> url))
throw new Exception("No download URL found! (Conversion not finished or failed)");
if (strpos($this -> data -> output -> url, 'http') === false)
$this -> data -> output -> url = "https:" . $this -> data -> output -> url;
$fp = fopen($target, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this -> data -> output -> url);
curl_setopt($ch, CURLOPT_FILE, $fp);
if (!curl_exec($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
fclose($fp);
}
private function req($url, $post = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
/*
* If you have SSL cert errors, try to disable SSL verifyer.
*/
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($post)) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$return = curl_exec($ch);
if ($return === FALSE) {
throw new Exception(curl_error($ch));
} else {
$json = json_decode($return);
if (isset($json -> error))
throw new Exception($json -> error);
return $json;
}
curl_close($ch);
}
}
?>