· 6 years ago · Feb 04, 2020, 03:32 AM
1<?php
2require __DIR__ . '/../vendor/autoload.php';
3require __DIR__ . '/../config.php';
4
5use Shotstack\Client\Api\EndpointsApi;
6use Shotstack\Client\Configuration;
7use Shotstack\Client\Model\Edit;
8use Shotstack\Client\Model\Output;
9use Shotstack\Client\Model\Soundtrack;
10use Shotstack\Client\Model\Timeline;
11use Shotstack\Client\Model\Track;
12use Shotstack\Client\Model\Clip;
13use Shotstack\Client\Model\TitleAsset;
14use Shotstack\Client\ObjectSerializer;
15
16// https://dev.ttbabes.com/application/third_party/shotstack-sdk-php/examples/text.php
17//header("Content-Type: plain/text");
18
19class TextDemo
20{
21 protected $apiKey;
22 protected $apiUrl = 'https://api.shotstack.io/stage';
23
24 public function __construct()
25 {
26 //if (empty(getenv('SHOTSTACK_KEY'))) {
27 // die("API Key is required. Set using: export SHOTSTACK_KEY=your_key_here\n");
28 //}
29
30 //if (!empty(getenv('SHOTSTACK_HOST'))) {
31 // $this->apiUrl = getenv('SHOTSTACK_HOST');
32 //}
33
34 //$this->apiKey = getenv('SHOTSTACK_KEY');
35
36 if ( !defined('SHOTSTACK_HOST') ) {
37 die("API Key is required");
38 }
39
40 if ( !defined('SHOTSTACK_KEY') ) {
41 die("API Key is required");
42 }
43
44 if( !empty(SHOTSTACK_HOST) ){
45 $this->apiUrl = SHOTSTACK_HOST;
46 }
47
48 $this->apiKey = SHOTSTACK_KEY;
49
50 }
51
52 public function render()
53 {
54 $config = Configuration::getDefaultConfiguration()
55 ->setHost($this->apiUrl)
56 ->setApiKey('x-api-key', $this->apiKey);
57
58 $client = new EndpointsApi(null, $config);
59
60 $soundtrack = new Soundtrack();
61 $soundtrack
62 ->setEffect("fadeInFadeOut")
63 ->setSrc("https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/music/disco.mp3");
64
65 $titleAsset = new TitleAsset();
66 $titleAsset
67 ->setStyle('minimal')
68 ->setText('Hello World');
69
70 $title = new Clip();
71 $title
72 ->setAsset($titleAsset)
73 ->setStart(0)
74 ->setLength(5)
75 ->setEffect('zoomIn');
76
77 $track1 = new Track();
78 $track1
79 ->setClips([$title]);
80
81 $timeline = new Timeline();
82 $timeline
83 ->setBackground("#000000")
84 ->setSoundtrack($soundtrack)
85 ->setTracks([$track1]);
86
87 $output = new Output();
88 $output
89 ->setFormat('mp4')
90 ->setResolution('sd');
91
92 $edit = new Edit();
93 $edit
94 ->setTimeline($timeline)
95 ->setOutput($output);
96
97 /*try {
98 //$response = $client->postRender($edit)->getResponse();
99
100 $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($edit));
101 //echo $httpBody;
102 //die();
103
104
105 } catch (Exception $e) {
106 die('Request failed: ' . $e->getMessage());
107 }*/
108
109 $client = new \GuzzleHttp\Client();
110
111 // Define array of request body.
112 $request_body = array();
113
114 try {
115
116 $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($edit));
117 $response = $client->request('POST','https://api.shotstack.io/stage/render', array(
118 'headers' => array(
119
120 'application/json',
121 'Accept' => 'application/json',
122 'x-api-key' => SHOTSTACK_KEY,
123 ),
124 'json' => $httpBody,
125 ));
126 print_r($response->getBody()->getContents());
127 }
128 catch (\GuzzleHttp\Exception\BadResponseException $e) {
129 // handle exception or api errors.
130 print_r($e->getMessage());
131
132 print_r( json_decode( $e->getResponse()->getBody()->getContents() , true ) );
133 }
134
135 //echo $response->getMessage() . "\n";
136 //echo ">> Now check the progress of your render by running:\n";
137 //echo ">> php examples/status.php " . $response->getId() . "\n";
138
139 }
140}
141
142$editor = new TextDemo();
143$editor->render();