youtube api json提供php错误

时间:2012-04-09 10:53:45

标签: php api youtube json

好的,我想要实现的是使用youtube api获取一些提要。 feed是json-c编码的。所以我尝试使用file_get_contents方法将json feedurl转换为字符串,json解码它。这是代码片段:

$feedURL = "https://gdata.youtube.com/feeds/api/videos?q=surfing&v=2&alt=jsonc";
$json = file_get_contents($feedURL,0,null,null);
$result = json_decode($json, true);
echo $result;
$id = $result->{'data'}->{'items'}[0]->{'id'};
echo "The video id is: ".$id;

但是我得到了这个愚蠢的错误警告:file_get_contents(https://gdata.youtube.com/feeds/api/videos?q=surfing&v=2&alt=jsonc)[function.file-get- contents] :无法打开流:第13行的/opt/lampp/htdocs/date.php中的连接被拒绝 注意:尝试在第16行的/opt/lampp/htdocs/date.php中获取非对象的属性 注意:尝试在第16行的/opt/lampp/htdocs/date.php中获取非对象的属性 注意:尝试在第16行的/opt/lampp/htdocs/date.php中获取非对象的属性

文件的名称是date.php,我在代理服务器后面的linux机器上的localhost上运行它。

我认为拒绝连接表明可能存在IP冲突或其他问题。我不知道。有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

尝试:


$url = 'http://www';

 $proxy = 'tcp://xxx:8080';

 $context = array(
    'http' => array(
        'proxy' => $proxy,
        'request_fulluri' => True,
        ),
    );

 $context = stream_context_create($context);

 $body = file_get_contents($url, False, $context);

foreach($result["data"]["items"] as $val) {
  echo $val["id"]."<br/>";
}

答案 1 :(得分:0)

您正在关闭连接的代理。通过使用file_get_contents

的代理访问问题已经有了答案

file_get_contents behind a proxy?

答案 2 :(得分:0)

您可以使用curl

$feedURL = "https://gdata.youtube.com/feeds/api/videos?q=surfing&v=2&alt=jsonc";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feedURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
//curl_setopt($ch,CURLOPT_PROXY,"x.x.x.x:8888"); // Proxy Use
$json = curl_exec($ch);

$result = json_decode($json, true);
foreach($result['data']['items'] as $items)
{
    var_dump($items['id']);
}