forked from cloudconvert/cloudconvert-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudConvert.class.php
More file actions
132 lines (112 loc) · 3.67 KB
/
CloudConvert.class.php
File metadata and controls
132 lines (112 loc) · 3.67 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
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
<?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
*/
const TIMEOUT = 120;
private $apikey;
private $url;
/*
* Constructor creates the Process ID.
* see: https://cloudconvert.org/page/api#start
*
*/
public function __construct($inputformat, $outputformat, $apikey = null) {
$this -> apikey = $apikey;
$data = $this -> req('https://api.cloudconvert.org/process', array(
'inputformat' => $inputformat,
'outputformat' => $outputformat,
'apikey' => $apikey
));
if (strpos($data -> url, 'http') === false)
$data -> url = "https:" . $data -> url;
$this -> url = $data -> url;
}
/*
* Does the actual conversion (server side)
*/
public function convert($filepath, $outputformat, $target) {
$file = pathinfo($filepath);
$this -> req($this -> url, array(
'input' => 'upload',
/*
* Specify advanced options like this:
*
* 'options[audio_bitrate]' => '128',
*
* see: https://cloudconvert.org/page/api#types
*
*/
'format' => $outputformat,
'file' => '@' . $filepath
));
$time = 0;
/*
* Check the status every second, up to timeout
*/
while ($time <= self::TIMEOUT) {
sleep(1);
$time++;
$data = $this -> req($this -> url);
if ($data -> step == 'error') {
throw new Exception($data -> message);
return false;
} elseif ($data -> step == 'finished' && isset($data -> output) && isset($data -> output -> url)) {
if (strpos($data -> output -> url, 'http') === false)
$data -> output -> url = "https:" . $data -> output -> url;
$this -> download($data -> output -> url, $target);
return true;
}
}
throw new Exception('Timeout');
return false;
}
public function getURL() {
return $this -> url;
}
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);
/*
* Remove this option for productive use!
* Therefore it may be necessary to store the certificate locally.
*/
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);
}
private function download($url, $target) {
$fp = fopen($target, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!curl_exec($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
fclose($fp);
}
}
?>