我想检查 YouTube 链接/ID 是否有效。它应该用 if/else 语句给我一个答案。
我试过这个:
<cfset variables.key = "my_key_value_here">
<cfset variables.headers = "https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=#variables.key#">
<cfif (IsArray(#variables.headers#) ? ReMatch('(http:\/\/)(?:www\.)?youtu(?:be\.com\/(?:watch\?|user\/|v\/|embed\/)\S+|\.be\/\S+)',#variables.headers#) : false)>
Correct Id!
<cfelse>
There is no video with that Id!
</cfif>
示例中的 ID 正确,应输出:“正确的 ID!”但它总是显示 else 语句。
谢谢你的帮忙!
最佳答案
我认为通过 youtube 数据 API 检查它来验证 youtube videoId 的最佳方法。以下是验证视频所需遵循的步骤。
完成上述步骤后,您可以使用 CFHTTP 在 ColdFusion 中简单地实现此 API。标签。
这是一个可以帮助您的代码片段。
<cfset variables.youtubeId = "EUcaCKWk83Q" />
<cfset variables.youTubeApiKey = "YOUR_API_KEY" />
<cfhttp url="https://www.googleapis.com/youtube/v3/videos?part=id&id=#youtubeId#&key=#youTubeApiKey#" >
<cfset response = deserializeJSON(cfhttp.fileContent) />
<cfif response.pageInfo.totalResults>
Correct Id!
<cfelse>
There is no video with that Id!
</cfif>
关于if-statement - 使用 ColdFusion 检查 Youtube ID 是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41258440/