好的,所以我要实现的是使用 youtube API 获取一些提要。 源是 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-content]:无法打开流:在第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 机器上的本地主机上运行它。
我认为连接被拒绝的事情表明可能存在 IP 冲突或其他什么。我不知道。有人可以帮我解决这个问题吗?
您可以使用
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']);
}
似乎代理您正在关闭连接。通过代理访问的问题已经有了答案file_get_contents
file_get_contents代理后面?
尝试:
$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/>";
}