PHP API在URL中返回带有额外“/”的URL

问题描述 投票:0回答:1

我有一个我正在使用的YouTube API(很明显归功于所有者),它返回MP4链接。 (例如:r5 --- sn.googlevideo,或任何谷歌链接)。我不得不编辑代码,因为创建者甚至说“这段代码不起作用”。所以我得到它的工作,但当它返回时,我得到一个看起来像https:\/\/link.com而不是https://link.com的链接。我带着额外的斜线去了链接,我得到https:////link.com,只是为了确定。

这是一个小Youtube链接提取器,因为我不能忍受看到网站弹出广告和其他恼人的链接,所以我想我会把一个放在我自己的网站上。

这是API的代码。

<?php

function YT_IN_DX($url){
    $cookie_file_path = "cookies.txt";
    $ch               = curl_init();
    $headers[]        = "Connection: Keep-Alive";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_URL, $url);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}
function YT_V_INFO($v){
    $url         = "https://www.youtube.com/get_video_info?video_id=$v";
    $html        = urldecode(YT_IN_DX($url));
    $video_links = Explode_Content('playabilityStatus', 'adSafetyReason', $html);
    $json        = str_replace("\u0026", "&", $video_links);
    $json        = '{"playabilityStatus' . $json . 'adSafetyReason":{"isEmbed":true}}';
    $array       = json_decode($json, true);
    if (isset($array["playabilityStatus"]["status"]) && $array["playabilityStatus"]["status"] == "UNPLAYABLE") {
        $data = array("error" => $array["playabilityStatus"]["status"]);
    }else{
        $formats = $array["streamingData"]["formats"];
        for ($a = 0; $a <= (count($formats) - 1); $a++){
            $data[] = array(
                "url" => $array["streamingData"]["formats"][$a]["url"],
                "mimeType" => $array["streamingData"]["formats"][$a]["mimeType"],
                "quality" => $array["streamingData"]["formats"][$a]["quality"],
                "qualityLabel" => $array["streamingData"]["formats"][$a]["qualityLabel"],
                "width" => $array["streamingData"]["formats"][$a]["width"],
                "height" => $array["streamingData"]["formats"][$a]["height"],
                "audioQuality" => $array["streamingData"]["formats"][$a]["audioQuality"],
                "approxDurationMs" => $array["streamingData"]["formats"][0]["approxDurationMs"]
            );
        }
    }
    return $data;
}
function Explode_Content($first, $last, $string)
{
    $exp = explode($first, $string);
    $exp = explode($last, $exp[1]);
    return $exp[0];
}
if(isset($_GET['url']) && $_GET['url'] != ""){
    parse_str( parse_url( $_GET['url'], PHP_URL_QUERY ), $vars );


    $id=$vars['v'];

    echo json_encode(YT_V_INFO($id),JSON_PRETTY_PRINT);

}else{
    @$myObj->error = true;
    $myObj->msg = "there is no youtube link";

    $myObj->madeBy = "El-zahaby";
    $myObj->instagram = "egy.js";
    $myJSON = json_encode($myObj,JSON_PRETTY_PRINT);

    echo $myJSON;

    echo "credit to el3zahaby!";

}

?>

我期望结果是一个很好的URL,但我得到一个额外的斜杠输出。

php
1个回答
1
投票

也许你必须逃避反斜杠

echo json_encode(YT_V_INFO($id),JSON_UNESCAPED_SLASHES);

或者你也可以手动删除反斜杠,用以下代码替换这部分

if(isset($_GET['url']) && $_GET['url'] != ""){
parse_str( parse_url( $_GET['url'], PHP_URL_QUERY ), $vars );
$id=$vars['v'];
$str = json_encode(YT_V_INFO($id),JSON_PRETTY_PRINT);
$str = str_replace('\\', '', $str);
echo $str;
© www.soinside.com 2019 - 2024. All rights reserved.