And*_*ndi 0 youtube coldfusion if-statement
我想检查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>
Run Code Online (Sandbox Code Playgroud)
示例中的ID是正确的,应输出:"正确的ID!" 但它始终显示else语句.
谢谢你的帮忙!
小智 5
我认为通过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>
Run Code Online (Sandbox Code Playgroud)
简单方案:
<cfset variables.key = "my_key_value_here">
<cfhttp method="Get" url="https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=#variables.key#">
<cfif cfhttp.statusCode EQ "200 OK">
Correct Id!
<cfelse>
There is no video with that Id!
</cfif>
<cfoutput>#cfhttp.statusCode#</cfoutput>
Run Code Online (Sandbox Code Playgroud)
我不需要RegEx,因为它始终是相同的链接,只是另一个ID.