-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYouTube.php
159 lines (91 loc) · 2.74 KB
/
YouTube.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
<?php
/**
* Class Search and get video information using youtube Api .v3
* @Package YouTube
* @version: 0.1
* @author Osama Salama <[email protected]>
* @copyright Copyright (c) 2016, Osama Salama
*/
class YouTube {
/**
* api key from https://console.developers.google.com/
* @var object
*/
private $Key ="00000000000000000000000000000000000000";
private $UrlQuery ="https://www.googleapis.com/youtube/v3/search?";
private $UrlVideo ="https://www.googleapis.com/youtube/v3/videos?";
private function ApiParse($Url){
if(function_exists('curl_version')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$Url);
$result=curl_exec($ch);
curl_close($ch);
}
else {
$result = file_get_contents($Url);
}
$data =json_decode($result, true);
if(isset($data['items']) && count($data['items']) > 0 ){
return $data;
} else {
return false;
}
}
//------------------------------------------------------------------
public function Search($query){
$Param = array('part'=>'snippet',
'q'=>$query,
'type'=>'video',
'maxResults'=>'30',
'key'=>$this->Key);
$Url = $this->UrlQuery . http_build_query($Param);
return $this->ApiParse($Url);
}
//---------------------------------------------------------------------------
public function VideoInfo($id){
$Url = $this->BuildUrlQuery($id);
$MetaData = $this->ApiParse($Url);
$DataResult=[];
if($MetaData!=false){
if(count($MetaData['items'])==1){
$DataResult=[
'id'=>$MetaData['items'][0]['id'],
'title'=>$MetaData['items'][0]['snippet']['title'],
'description'=>$MetaData['items'][0]['snippet']['description'],
'thumb'=>$MetaData['items'][0]['snippet']['thumbnails']['default']['url'],
'duration'=>$this->duration($id),
];
return $DataResult;
}
} else {
return null;
}
}
private function duration($id){
$Url = $this->BuildUrlQuery($id,'contentDetails');
$MetaData = $this->ApiParse($Url);
if($MetaData!=false){
if(count($MetaData['items'])==1){
return $this->FormatDuration($MetaData['items'][0]['contentDetails']['duration']);
}
} else {
return null;
}
}
private function BuildUrlQuery($VideoId,$PartType = 'snippet'){
$Param = array('id'=>$VideoId,
'part'=>$PartType,
'key'=>$this->Key);
$Url = $this->UrlVideo . http_build_query($Param);
return $Url;
}
//---------------------------------------------------------------------
private function FormatDuration($duration){
$FormatTime = new DateTime('@0');
$FormatTime->add(new DateInterval($duration));
return $FormatTime->format('H:i:s');
}
//
}