可能在函数之外的全局file_get_contents变量?

时间:2015-05-25 05:03:59

标签: php

我有一个json api我正在尝试提供几个功能。我不完全确定如何使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会更明智吗?感谢。

2 个答案:

答案 0 :(得分:2)

如果$json应该保持不变,那么我相信您最好创建一个类,因为您需要为视频的ID提供$getVideoID,另外您可以方便地更改视频:)

<?php
class MyVideoClass {
    private $json = null;

    public function __construct($videoID) {
        $this->changeVideo($videoID);
    }

    public function changeVideo($videoID) {
        $json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$videoID."&key={MY_KEY}&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
        $this->json = json_decode($json_output, true);
    }

    function getVideoTitle() {
        $video_title = $this->json['items'][0]['snippet']['title']; // Video Title
        return $video_title;
    }

    function getVideoDesc() {
        $video_description = $this->json['items'][0]['snippet']['description']; //Description
        return $video_description;
    }
}

// somewhere:
$myVideo = new MyVideoClass($yourVideoID);
echo $myVideo->getVideoTitle();
echo $myVideo->getVideoDesc();

// sometime else
$myVideo->changeVideo($anotherVideoID);
echo $myVideo->getVideoTitle();
echo $myVideo->getVideoDesc();

答案 1 :(得分:-1)

这里是我为你写的

<?php
class Video{
private $data;
public function __construct($videoId, $key){
    $json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$videoId."&key=".$key."&fields=items(id,snippet(title,description),statistics)&part=snippet,statistics");
    isset($json_output) ? $this->data = $json_output : $this->data = false;
}
public function getTitle(){
    if($this->data){
        $video_title = $this->data['items'][0]['snippet']['title']; // Video Description
        return $video_title;
    }else{
        return false;
    }
}
public function getDesc(){
    if($this->data){
        $video_desc = $this->data['items'][0]['snippet']['description']; // Video Title
        return $video_desc;
    }else{
        return false;
     }
  }

public function change($videoId, $key){
    $this->__construct($videoId, $key);
 }
}    

用法示例:

include('video.php'); // if you going to save the class in separate file
$video = new Video('Video-Id-Here', 'your-key');
echo $video->getTitle().'<br/>';
echo $video->getDesc();
//new video
$video->change('new-video-id', 'new-key');
echo $video->getTitle().'<br/>';
echo $video->getDesc();

您可以在课程中添加更多功能