-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.php
211 lines (131 loc) · 5.08 KB
/
request.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
//get list video
require_once 'Global.php';
// header('Content-Type: application/json');
$inst1=UserFactory::Instance();
// $inst1->result();
$youtube = $inst1->youtube;
$client = $inst1->client;
function getListVideoID(Google_Service_YouTube $youtube) {
// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id', array(
'maxResults' => '50',
'forMine' => true,
'type' => 'video',
'pageToken' => $_POST['token']
));
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
$listVideosId = '';
$pageToken = $searchResponse['nextPageToken'];
foreach ($searchResponse['items'] as $searchResult) {
if ($listVideosId == '') {
$listVideosId = $searchResult['id']['videoId'];
} else{
$listVideosId .= ', ' .$searchResult['id']['videoId'];
}
// echo $searchResult['id']['videoId']." ";
}
if (count($searchResult) > 0) {
getVideoDetail($youtube, $listVideosId, $pageToken);
} else{
sendErrorMessage('no data return');
}
}
function getVideoDetail(Google_Service_YouTube $youtube, $videoId, $pageToken) {
// Call the YouTube Data API's videos.list method to retrieve videos.
$videos = $youtube->videos->listVideos("id, status, snippet", array(
'id' => $videoId
));
// If $videos is empty, the specified video was not found.
if (empty($videos)) {
echo '<h3>Can\'t find a video with video id: %s</h3>'. $videoId;
} else {
// Since the request specified a video ID, the response only
// $array = json_decode(json_encode($videos), true);
// var_dump($array);
// echo json_encode($array);
// print_r(json_decode($videos, true));
// echo json_encode(object_to_array($videos));
$listPriveVideo = "";
$listVideosId = array();
$listVideosTitle = array();
$listVideoPrivate = array();
foreach ($videos['items'] as $searchResult) {
if ($searchResult['status']['privacyStatus'] == "private") {
if ($listPriveVideo == '') {
$listPriveVideo = $searchResult['id'];
} else{
$listPriveVideo .= ',' .$searchResult['id'];
}
}
array_push($listVideosId, $searchResult['id']);
array_push($listVideosTitle, $searchResult['snippet']['title']);
array_push($listVideoPrivate, $searchResult['status']['privacyStatus']);
}
$res = array('status' => 1,
'nextPageToken' => $pageToken,
'privateVideo' => $listPriveVideo,
'listId' => $listVideosId,
'listTitle' => $listVideosTitle,
'private' => $listVideoPrivate
);
echo json_encode($res);
}
}
function setVideoTime(Google_Service_YouTube $youtube, $videoId, $publicDate) {
// Call the YouTube Data API's videos.list method to retrieve videos.
// Call the API's videos.list method to retrieve the video resource.
$listResponse = $youtube->videos->listVideos("status", array('id'=>$videoId));
// If $listResponse is empty, the specified video was not found.
if (empty($listResponse)) {
sendErrorMessage('Can\'t find a video');
} else {
// Since the request specified a video ID, the response only
// contains one video resource.
$video = $listResponse[0];
$videoStatus = $video['status'];
$videoStatus->privacyStatus = 'private'; #privacyStatus options are public, private, and unlisted
$videoStatus->publishAt = $publicDate;//'2016-11-23T07:29:00.50Z';
$video->setStatus($videoStatus);
$updateResponse = $youtube->videos->update('status', $video);
$message .= '<li> Update success for video id: '.$updateResponse['id'].' </li>';
$res = array('status' => 1,
'message' => $message
);
echo json_encode($res);
}
}
function sendErrorMessage ($mes){
$res = array('status' => 0,
'message' => $mes,
);
echo json_encode($res);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
// This code executes if the user ays tenters an action in the form
// and submits the form. Otherwise, the page displhe form above.
if($client->isAccessTokenExpired()) {
// echo ("Token expired ");
}
if (isset($_POST['token'])) {
try {
getListVideoID($youtube);
} catch (Google_Service_Exception $e) {
sendErrorMessage($e->getMessage());
} catch (Google_Exception $e) {
sendErrorMessage($e->getMessage());
}
} else if (isset($_POST['videoId']) && isset($_POST['time'])) {
try {
setVideoTime($youtube, $_POST['videoId'], $_POST['time']);
} catch (Google_Service_Exception $e) {
sendErrorMessage($e->getMessage());
} catch (Google_Exception $e) {
sendErrorMessage($e->getMessage());
}
}
}
?>