У меня есть json api. Я пытаюсь прокормить несколько функций. Я не совсем уверен, как сделать глобальный file_get_contents
так, чтобы к нему можно было получить доступ сразу несколькими функциями.глобальная переменная file_get_contents вне функции?
Вот мой текущий код PHP:
function getVideoTitle($getVideoID) {
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$getVideoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
$json = json_decode($json_output, true);
$video_title = $json['items'][0]['snippet']['title']; // Video Title
return $video_title;
}
function getVideoDesc($getVideoID) {
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$getVideoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
$json = json_decode($json_output, true);
$video_description = $json['items'][0]['snippet']['description']; //Description
return $video_description;
}
echo getVideoTitle($getVideoID);
echo getVideoDesc($getVideoID);
Если удалить два $json
переменных и поместить их вне функции я получаю ошибку, что не может найти переменную $json
.
Кроме того, будет ли это глобальная переменная заставлять ее работать быстрее? Получение API в настоящее время выполняется довольно медленно. Было бы разумнее перейти на Javascript ..? Благодарю.