1+ <?php
2+ use TusPhp \Tus \Client ;
3+ use Vimeo \Upload \TusClient ;
4+ use Vimeo \Upload \TusClientFactory ;
5+ use Vimeo \Vimeo ;
6+ use Vimeo \Exceptions \VimeoUploadException ;
7+
8+ /**
9+ * Copyright 2022 Vimeo
10+ *
11+ * Licensed under the Apache License, Version 2.0 (the "License");
12+ * you may not use this file except in compliance with the License.
13+ * You may obtain a copy of the License at
14+ *
15+ * http://www.apache.org/licenses/LICENSE-2.0
16+ *
17+ * Unless required by applicable law or agreed to in writing, software
18+ * distributed under the License is distributed on an "AS IS" BASIS,
19+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+ * See the License for the specific language governing permissions and
21+ * limitations under the License.
22+ */
23+ $ config = require (__DIR__ . '/init.php ' );
24+
25+ class LocalCacheTusClientFactory extends TusClientFactory
26+ {
27+ public function getTusClient (string $ base_uri , string $ url ): Client
28+ {
29+ $ client = new TusClient ($ base_uri );
30+ $ client ->setUrl ($ url );
31+ $ client ->setCache (new \TusPhp \Cache \FileStore ('./cache ' ));
32+ return $ client ;
33+ }
34+ }
35+
36+ if (empty ($ config ['access_token ' ])) {
37+ throw new Exception (
38+ 'You can not upload a file without an access token. You can find this token on your app page, or generate ' .
39+ 'one using `auth.php`. '
40+ );
41+ }
42+
43+ // Instantiate the library with your client id, secret and access token (pulled from dev site), pass in the TusClientFactory
44+ $ lib = new Vimeo ($ config ['client_id ' ], $ config ['client_secret ' ], $ config ['access_token ' ], new LocalCacheTusClientFactory ());
45+
46+ // Create a variable with a hard coded path to your file system
47+ $ file_name = '<fully qualified path> ' ;
48+
49+ echo 'Uploading: ' . $ file_name . "\n" ;
50+
51+ try {
52+ // Upload the file and include the video title and description.
53+ $ uri = $ lib ->upload ($ file_name , array (
54+ 'name ' => 'Vimeo API SDK test upload ' ,
55+ 'description ' => "This video was uploaded through the Vimeo API's PHP SDK. "
56+ ));
57+
58+ // Get the metadata response from the upload and log out the Vimeo.com url
59+ $ video_data = $ lib ->request ($ uri . '?fields=link ' );
60+ echo '" ' . $ file_name . ' has been uploaded to ' . $ video_data ['body ' ]['link ' ] . "\n" ;
61+
62+ // Make an API call to edit the title and description of the video.
63+ $ lib ->request ($ uri , array (
64+ 'name ' => 'Vimeo API SDK test edit ' ,
65+ 'description ' => "This video was edited through the Vimeo API's PHP SDK. " ,
66+ ), 'PATCH ' );
67+
68+ echo 'The title and description for ' . $ uri . ' has been edited. ' . "\n" ;
69+
70+ // Make an API call to see if the video is finished transcoding.
71+ $ video_data = $ lib ->request ($ uri . '?fields=transcode.status ' );
72+ echo 'The transcode status for ' . $ uri . ' is: ' . $ video_data ['body ' ]['transcode ' ]['status ' ] . "\n" ;
73+ } catch (VimeoUploadException $ e ) {
74+ // We may have had an error. We can't resolve it here necessarily, so report it to the user.
75+ echo 'Error uploading ' . $ file_name . "\n" ;
76+ echo 'Server reported: ' . $ e ->getMessage () . "\n" ;
77+ } catch (VimeoRequestException $ e ) {
78+ echo 'There was an error making the request. ' . "\n" ;
79+ echo 'Server reported: ' . $ e ->getMessage () . "\n" ;
80+ }
0 commit comments