2017-02-11 111 views
0

此问题不是重复的。我的问题没有显示错误的数据部分,因为它在标记的文章中,但作为一个细心的用户反映,“您的问题中的具体问题是,您使用的URL只检索实际的视频,而不是包含视频。”无法从YouTube视频对象获取视频标题

我需要得到一个YouTube视频的标题为Magento的网上商店。我的代码是正确的,现在以下几点:

$videocode = $_helper->productAttribute($_product, $_data['value'], $_data['code']); 
$url = "http://www.youtube.com/v/".$videocode."?hl=en_US&version=3"; 
echo $url; 
$videoTitle = file_get_contents($url); 
try { 
    if ($videoTitle) { 
     preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo); 
     echo $titleOfVideo[1]; 
    } else { 
     return false; 
    } 
} catch (Exception $e) { 
    echo 'Caught exception: ', $e->getMessage(), "\n"; 
} 

echo $url;返回正确的视频网址。

但事后,如果我尝试的var_dump的$ videoTitle,它返回像CWS[%xڍZ[{�Fzƀ�@�:Ҷ��v"�uh �H�(E�9����ή� $1=�馧m�=nϽko�z������$��M��_l�oR�lﳶ�����~�� �C)��4��4ʤ[SI�nM�"�.��b�yB��ᵵ��5�u�8�\�,我指的是一群人。 但它不会返回false。

为$ titleOfVideo返回array(0) { }。 否则,如果我不尝试回显或var_dump任何东西,它不会为我回馈任何东西。 我在这里错过了什么?


被引导,使经过一番研究,我的代码看起来如下:

$videocode = $_helper->productAttribute($_product, $_data['value'], $_data['code']); 
$videocode = explode('&feature=youtu.be', $videocode); 
$url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id‌​=".$videocode[0]."&key=".$apikey; 
echo $url; 
$json = file_get_contents($url); 
var_dump($json); //bool(false) 
$json_data = json_decode($json, true); 
if ($json_data) { 
    print_r($json_data); 
} else { 
    return false; 
} 

的视频ID是正确的。但我的$json返回false。我还想念什么?

+0

可能的[使用YouTube API检索并显示YouTube视频说明,标题使用id](http://stackoverflow.com/questions/34022332/retrieve-and-display-youtube-video-description-title-by -its-id-using-youtube-ap) – NineBerry

+1

使用官方API而不是试图从实况网站读取元数据。你的问题中的具体问题是你使用的URL只能检索实际的视频,而不是包含视频的页面。 – NineBerry

+0

@NineBerry在YouTube API工作之后看了一下后,我断定,如果'$ videocode'是我关于视频的唯一细节,那么执行所需操作是不够的。我对吗? – sklrboy

回答

1

使用谷歌API来访问YouTube视频元数据。

首先,您必须向Google注册以接收API密钥。按照these instructions这样做。

然后你就可以构建一个URL来检索标题是这样的:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=dRvS-tLCi5Y&key=YourAPIKey 

使用您的API密钥到位。

这将返回以下JSON:

{ 
"kind": "youtube#videoListResponse", 
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/nmxGC9Qyfl2o0ayvTd9DwRpHySo\"", 
"pageInfo": { 
    "totalResults": 1, 
    "resultsPerPage": 1 
}, 
"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/WzAkhb8NRCtBYcATr8BHzcJJ8sE\"", 
    "id": "dRvS-tLCi5Y", 
    "snippet": { 
    "publishedAt": "2015-11-17T20:49:02.000Z", 
    "channelId": "UCuTn1q6pg2kKRLAQIWSEO5g", 
    "title": "Womanizer - W500 Pro Klitoris Stimulator NYHED", 
    "description": "Womanizers unikke teknologi stimulerer din klitoris på en helt unik måde. Teknologien blander en sugende og pustende fornemmelse på klitoris og evt brystvorter, der sørger for \"orgasme garanti\". Stimulatoren har 8 unikke indstilinger, der giver dig en fantastisk oplevelse. Stimulatoren er formet, så den ligger godt i hånden. Mundstykket kan udskiftes og rengøres. Genopladelig med medfølgende USB kabel.\n\nGod fornøjelse :)\n\nKøb i dag hos: http://www.winkwink.dk/womanizer-w500-pro-klitoris-stimulator-nyhed.html", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/dRvS-tLCi5Y/default.jpg", 
     "width": 120, 
     "height": 90 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/dRvS-tLCi5Y/mqdefault.jpg", 
     "width": 320, 
     "height": 180 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/dRvS-tLCi5Y/hqdefault.jpg", 
     "width": 480, 
     "height": 360 
    }, 
    "standard": { 
     "url": "https://i.ytimg.com/vi/dRvS-tLCi5Y/sddefault.jpg", 
     "width": 640, 
     "height": 480 
    }, 
    "maxres": { 
     "url": "https://i.ytimg.com/vi/dRvS-tLCi5Y/maxresdefault.jpg", 
     "width": 1280, 
     "height": 720 
    } 
    }, 
    "channelTitle": "Winkwink.dk", 
    "tags": [ 
    "#sexlegetøj", 
    "#winkwinkdk", 
    "#sex", 
    "#winkwink", 
    "#womanizer", 
    "#julegave", 
    "#lovewinkwinkdk", 
    "#stimulator", 
    "#klitoris", 
    "Clitoris (Anatomical Structure)" 
    ], 
    "categoryId": "22", 
    "liveBroadcastContent": "none", 
    "localized": { 
    "title": "Womanizer - W500 Pro Klitoris Stimulator NYHED", 
    "description": "Womanizers unikke teknologi stimulerer din klitoris på en helt unik måde. Teknologien blander en sugende og pustende fornemmelse på klitoris og evt brystvorter, der sørger for \"orgasme garanti\". Stimulatoren har 8 unikke indstilinger, der giver dig en fantastisk oplevelse. Stimulatoren er formet, så den ligger godt i hånden. Mundstykket kan udskiftes og rengøres. Genopladelig med medfølgende USB kabel.\n\nGod fornøjelse :)\n\nKøb i dag hos: http://www.winkwink.dk/womanizer-w500-pro-klitoris-stimulator-nyhed.html" 
    }, 
    "defaultAudioLanguage": "da" 
    } 
    } 
] 
} 

然后,您可以阅读题是这样的:

$json_data = json_decode($JSON, true); 
$title = $json_data['items'][0]['snippet']['title']; 

注意,使用YouTube API是首选,因为任何企图利用看视频元数据的实时网站将导致YouTube识别这些请求并阻止呼叫(显示验证码而不是提供真实的视频代码)

+0

这很有趣。它正确地打印了url,但是在复制两个url进行比较之后,从脚本返回的mine在'&id'之后和之前有一个额外的'%E2%80%8C%E2%80%8B'实际ID ... – sklrboy

+0

正如我所说,那是因为你从这里的评论中复制它。在堆栈溢出 – NineBerry

+0

的注释中存在换行符时,插入特殊字符。它不是来自页面本身,由'echo $ url;'返回。也许'$ videocode [0]'不是这里的最佳解决方案... – sklrboy