RapidAPI是一个领先的API平台,旨在为开发者提供便捷的API发现、集成和管理服务。 作为一个全球性的API市场,RapidAPI汇集了数千个来自不同提供商的API,涵盖了广泛的技术和应用领域。 RapidAPI被500多万开发人员用于查找、测试和连接到数千个API——所有这些都使用一个API密钥。
注册帐号后搜索YouTube会出现很多API,随便找到一条就可以,缺点:基本每日都有限制50-100条/天
我的源码:
<?php
/*================================================
作者信息: 伟航工作室 [QQ:327954269] [www.weihangapi.cn]
=================================================*/
header('Content-Type: application/json');
$url = "https://youtu.be/ecAZThYr6Xo?si=nSUuQHNhrT20LZ3H";
preg_match('/youtu.be\/(.*)\?/', $url, $id);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://youtube-search-and-download.p.rapidapi.com/video?id=".$id[1],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: youtube-search-and-download.p.rapidapi.com",
"X-RapidAPI-Key: d4a55bfbf4msh4f7639aad855136p161afcjsnd9c251b4d75d"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
//echo $response;
/*/if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}/*/
$data = json_decode($response, true);
//var_dump($data);
$tit = $data["videoDetails"]["title"];
//echo $tit;
$videourl = $data["streamingData"]["adaptiveFormats"][0]["url"];
//echo $videourl;
$img =$data["videoDetails"]["thumbnail"]["thumbnails"][0]["url"];
//echo $img;
$data1 = array(
'code' => 200,
'msg' => '解析成功',
'From' => 'weihangapi.cn',
'title' => $tit,
'cover' => $img,
'url' => $videourl
);
echo json_encode($data1);
获得json:
{"code":200,"msg":"\u89e3\u6790\u6210\u529f","From":"weihangapi.cn","title":"\u3010\u5c0f\u5c9b\u6d6a\u5439\u3011\u6bdb\u6cfd\u4e1c\u4f201\uff1a\u53db\u9006\u4e2d\u4e8c\u5c11\u5e74\u4ece\u5c71\u6751\u8d70\u5411\u57ce\u5e02\uff0c\u804a\u804a\u5c11\u5e74\u6bdb\u6cfd\u4e1c\u7684\u4e0d\u8db3\uff0c\u6210\u957f\uff0c\u5a5a\u59fb\u4ee5\u53ca\u5bfc\u5e08","cover":"https:\/\/i.ytimg.com\/vi\/ecAZThYr6Xo\/default.jpg","url":"https:\/\/rr4---sn-q0c7rn7d.googlevideo.com\/videoplayback?expire=1722845679&ei=jzWwZtjYF7jfhcIPruyc8Qs&ip=3.255.239.115&id=o-AEnCcp4HTX9_wmvde74qOKqNZ4uq7ub-ntJ_BJqCdpoC&itag=137&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&mh=Ns&mm=31%2C26&mn=sn-q0c7rn7d%2Csn-5hnednss&ms=au%2Conr&mv=m&mvi=4&pl=24&initcwndbps=651250&bui=AXc671JCZSCnLa-e69q3OGoR5Soqn0p3YlmKtRZ_axqErEdMWyG15NdConKt8zxb_EnzZ51ygScwaPkD&vprv=1&mime=video%2Fmp4&rqh=1&gir=yes&clen=74440062&dur=1185.000&lmt=1639396991696403&mt=1722823980&fvip=2&keepalive=yes&lmw=1&c=ANDROID_TV&txp=5535434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cbui%2Cvprv%2Cmime%2Crqh%2Cgir%2Cclen%2Cdur%2Clmt&sig=AJfQdSswRgIhAK0rpqpQL5PiwSkucaTOur_0feRr3hZe2AIVUBnv4GORAiEA6hxu3R_1XmQHFiEJVNAeuBi2r1IvaTeQ1apW4xvVCDE%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AGtxev0wRAIgC2cWRCLt_p1HC_VTYsnW7BBqqwD-FrgtAYSjM9NxAkICIBCh12RCdtdtUIJXG3F7DMieRB4lpWNtmluoQYRDoI4J"}