-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsearch.php
58 lines (46 loc) · 1.57 KB
/
search.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
<?php
use MichaelBelgium\YoutubeConverter\Config;
require_once __DIR__ . '/vendor/autoload.php';
$result = array("error" => false, "message" => null, "results" => array());
header("Content-Type: application/json");
if (isset($_GET["q"]) && !empty($_GET["q"]))
{
$max_results = Config::MAX_RESULTS;
if(isset($_GET["max_results"]) && !empty($_GET["max_results"]))
$max_results = $_GET["max_results"];
$client = new Google_Client();
$client->setDeveloperKey(Config::API_KEY);
$youtube_service = new Google_Service_YouTube($client);
try
{
$search = $youtube_service->search->listSearch("id,snippet", array(
"q" => $_GET["q"],
"maxResults" => $max_results,
"type" => "video"
));
foreach ($search["items"] as $searchResult)
{
$result["results"][] = array(
"id" => $searchResult["id"]["videoId"],
"channel" => $searchResult["snippet"]["channelTitle"],
"title" => $searchResult["snippet"]["title"],
"full_link" => "https://youtube.com/watch?v=".$searchResult["id"]["videoId"]
);
}
$result["error"] = false;
}
catch (Exception $exception)
{
http_response_code(400);
$json = json_decode($exception->getMessage());
$result["error"] = true;
$result["message"] = $json->error->message;
}
}
else
{
http_response_code(400);
$result["error"] = true;
$result["message"] = "Invalid request. Missing 'q' parameter.";
}
echo json_encode($result);