Skip to content

Commit 48be8ce

Browse files
committed
v1
1 parent f27a57b commit 48be8ce

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "json2video/json2video-php-sdk",
3+
"description": "PHP SDK for JSON2Video API: Create videos programmatically",
4+
"type": "SDK",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Json2video\\json2video-php-sdk\\": "src/"
9+
}
10+
},
11+
"require": {}
12+
}

src/json2video-php-sdk.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/*
4+
5+
JSON2Video PHP SDK
6+
7+
This simple SDK is a wrapper for calling JSON2Video API
8+
JSON2Video API allows you to create and edit videos programmatically
9+
10+
Documentation: https://json2video.com/docs/sdk
11+
12+
*/
13+
14+
namespace JSON2Video;
15+
16+
class Base {
17+
18+
protected $object, $properties;
19+
20+
public function __get($property) {
21+
$property = strtolower($property);
22+
if (in_array($property, $this->properties) && isset($this->object[$property])) {
23+
return $this->object[$property];
24+
}
25+
26+
return null;
27+
}
28+
29+
public function __set($property, $value) {
30+
$property = strtolower($property);
31+
if (in_array($property, $this->properties)) {
32+
$property = strtolower(str_replace('_', '-', $property));
33+
$this->object[$property] = $value;
34+
return $value;
35+
}
36+
37+
return null;
38+
}
39+
40+
public function addElement($element=null) {
41+
if ($element && is_array($element)) {
42+
if (!isset($this->object['elements'])) $this->object['elements'] = [];
43+
$this->object['elements'][] = $element;
44+
return true;
45+
}
46+
return false;
47+
}
48+
49+
public function getJSON() {
50+
return json_encode($this->object, JSON_PRETTY_PRINT);
51+
}
52+
53+
public function getObject() {
54+
return $this->object;
55+
}
56+
}
57+
58+
59+
class Scene extends Base {
60+
protected $properties = ['comment', 'background_color', 'duration', 'cache'];
61+
62+
protected $object = [];
63+
64+
public function setTransition($style=null, $duration=null, $type=null) {
65+
if ($style || $duration || $type) {
66+
if (!isset($this->object['transition'])) $this->object['transition'] = [];
67+
if (!is_null($style)) $this->object['transition']['style'] = $style;
68+
if (!is_null($duration)) $this->object['transition']['duration'] = $duration;
69+
if (!is_null($type)) $this->object['transition']['type'] = $type;
70+
}
71+
}
72+
73+
}
74+
75+
class Movie extends Base {
76+
private $api_url = 'https://api.json2video.com/v1/movies';
77+
protected $properties = ['comment', 'project', 'width', 'height', 'resolution', 'quality', 'fps', 'cache'];
78+
79+
private $apikey = null;
80+
81+
protected $object = [];
82+
83+
public function setAPIKey($apikey) {
84+
$this->apikey = $apikey;
85+
}
86+
87+
public function addScene($scene=null) {
88+
if ($scene && is_a($scene, 'JSON2Video\Scene')) {
89+
if (!isset($this->object['scenes'])) $this->object['scenes'] = [];
90+
$this->object['scenes'][] = $scene->getObject();
91+
return true;
92+
}
93+
else throw new \Exception('Invalid scene');
94+
return false;
95+
}
96+
97+
private function fetch(string $method, string $url, string $body, array $headers = []) {
98+
$context = stream_context_create([
99+
"http" => [
100+
"method" => $method,
101+
"header" => implode("\r\n", $headers),
102+
"content" => $body,
103+
"ignore_errors" => true,
104+
],
105+
]);
106+
107+
$response = file_get_contents($url, false, $context);
108+
109+
$status_line = $http_response_header[0];
110+
111+
preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match);
112+
113+
$status = $match[1];
114+
115+
return [
116+
'status' => $status,
117+
'message' => $status_line,
118+
'response' => $response
119+
];
120+
}
121+
122+
public function render() {
123+
124+
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
125+
126+
$postdata = json_encode($this->object);
127+
if (is_null($postdata)) {
128+
throw new \Exception('Invalid movie settings');
129+
}
130+
131+
$response = $this->fetch('POST', $this->api_url, $postdata, [
132+
"Content-Type: application/json",
133+
"x-api-key: {$this->apikey}"
134+
]);
135+
136+
if ($response) {
137+
if ($response['status']=='200') return json_decode($response['response'], true);
138+
elseif ($response['status']=='400') {
139+
$api_response = json_decode($response['response'], true);
140+
throw new \Exception('JSON Syntax error: ' . ($api_response['message'] ?? 'Unknown error'));
141+
}
142+
else {
143+
144+
throw new \Exception('API error: ' . ($response['message'] ?? 'Unknown error'));
145+
}
146+
}
147+
else throw new \Error('SDK error');
148+
149+
return false;
150+
}
151+
152+
public function getStatus() {
153+
154+
if (empty($this->apikey)) throw new \Exception('Invalid API Key');
155+
if (empty($this->object['project'])) throw new \Exception('Project ID not set');
156+
157+
$url = $this->api_url . '?project=' . $this->object['project'];
158+
159+
$status = $this->fetch('GET', $url, '', [
160+
"x-api-key: {$this->apikey}"
161+
]);
162+
163+
if ($status) {
164+
if ($status['status']=='200') return json_decode($status['response'], true);
165+
else {
166+
throw new \Exception('API error: ' . ($status['message'] ?? 'Unknown error'));
167+
}
168+
}
169+
else throw new \Error('SDK error');
170+
171+
return false;
172+
}
173+
}

0 commit comments

Comments
 (0)