2

在我的一个应用程序中,我正在保存一个 YouTube 视频的 id……比如“A4fR3sDproE”。我必须在应用程序中显示它的标题。我得到了以下代码来获取它的标题,而且它工作正常。

现在的问题是如果发生任何错误(在删除视频的情况下)PHP 会显示错误。我只是添加了一个条件。但它仍然显示错误。

foreach($videos as $video) {

    $video_id = $video->videos;
    if($content=file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id)) {
        parse_str($content, $ytarr);

        $myvideos[$i]['video_title']=$ytarr['title'];

    }
    else
        $myvideos[$i]['video_title']="No title";

    $i++;
}

return $myvideos;

如果出现错误,它会因以下情况而死:

严重性:警告

消息:file_get_contents(http://youtube.com/get_video_info?video_id=A4fR3sDprOE)[function.file-get-contents]:打开流失败:HTTP 请求失败!HTTP/1.0 402 需要付款

文件名:models/webs.php

行号:128

4

7 回答 7

11

将file_get_contents()与远程 URL一起使用是不安全的。在 YouTube API 2.0 中使用 cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);

if ($response) {
    $xml   = new SimpleXMLElement($response);
    $title = (string) $xml->title;
} else {
    // Error handling.
}
于 2012-03-12T09:44:05.510 回答
6

这是我的解决方案。它很短。

$id = "VIDEO ID";
$videoTitle = file_get_contents("http://gdata.youtube.com/feeds/api/videos/${id}?v=2&fields=title");

preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo);
$videoTitle = $titleOfVideo[1];
于 2013-03-31T20:02:47.253 回答
3

在 file_get_contents 之前使用错误控制运算符可能会起作用。

就像是:

if($content = @file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id))

if它应该删除错误并使用它在您的语句中返回 false 。

否则,您可以只使用 try/catch 语句(请参阅Exceptions):

try{
    // Code
}
catch (Exception $e){
    // Else code
}
于 2012-03-12T09:33:35.483 回答
1

我相信您的托管服务提供商出于安全目的禁用了 file_get_contents。您应该使用cURL

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=" . $video_id);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    /* Parse YouTube's response as you like below */
?>
于 2012-03-12T09:36:57.917 回答
1

尝试:

// Use @ to suppress warnings
$content = @file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id);
if($content===FALSE) {
    .. Handle error here
}
else{
    ..The rest of your code
}
于 2012-03-12T09:38:31.723 回答
1

我想补充一点,此处看到的特定错误(HTTP 402 - 需要付款,否则是服务器通常未实现的 HTTP 状态代码)是 YouTube 在确定“过多”金额时返回的内容的流量来自您的 IP 地址(或 IP 地址范围——他们更喜欢经常阻止 OVH 的 IP 地址范围[1] [2]

因此,如果您以编程方式(使用 PHP 或其他方式)访问 youtube.com(而不是 API),您最终可能会发现自己遇到了这个错误。YouTube 通常从向来自“过多流量”IP 地址的请求提供验证码开始,但如果您没有完成它们(您的 PHP 脚本不会),他们将切换到这些无用的 402 响应,基本上没有追索权 - YouTube没有确切的客户支持服务台可以打电话,如果由于 IP 地址阻止您实际上无法访问他们的任何网站,那么您联系他们的机会就更少了。

其他参考: http://productforums.google.com/forum/#! topic /youtube/
tR4WkNBPnUo http://productforums.google.com/forum/?fromgroups=#!topic/youtube/179aboankVg

于 2013-02-07T21:31:05.090 回答
0

我的解决方案是:

$xmlInfoVideo = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/" . $videoId . "?v=2&fields=title");

foreach($xmlInfoVideo->children() as $title) {
    $videoTitle = strtoupper((string) $title); 
}

这将获得视频的标题。

于 2013-12-20T10:54:10.210 回答