2014-10-20 52 views
-1

这里是脚本的完整来源:https://gist.github.com/x10hassan/454347585de3c2312f1c#file-cipher我如何知道arr变量来自哪里?

关于脚本和小背景:

这个脚本是用来获取和解码的YouTube下载链接,它在运作的瞬间只是结构码好,服务器的目的,我试图将其转换为功能。

我的问题:

看看完整的源代码有foreach语句上线94

foreach ($links as $link) { 
        parse_str($link,$linkarr); 

        // parse link array one by one and decrypt the signature 
        $dlinks[$linkarr['itag']] = $linkarr['url'] . "&signature=" . decrypt($linkarr['s'],$algo); 
        echo $linkarr['itag'].'<br />'; 
        echo $dlinks[$linkarr['itag']].'<br /><br />'; 
       } 

有一个$ linkarr变量在哪里呢它来自什么用处呢它服务,编译的结果可以在这里找到:http://yt.hassannaseer.com/sb/cipher.php

我想创建一个函数,它接受视频ID并返回一个关键数组关联包含ng“itag”和价值“解码youtube下载链接”?有人可以帮助我吗?

+2

你看看[parse_str()](http://www.php.net/manual/en/function.parse-str.php)的文档,看看'$ linkarr'来自何处' – 2014-10-20 10:53:41

+0

@MarkBaker I得到那一个,你能指导我的功能问题吗? – user3676425 2014-10-20 11:23:39

回答

0

请找到完整的代码在这里:Github/bitnol

仅供参考检查:

<?php 

/** 
* Author: Akhilesh Chandra Gupta 
* Author URI: http://www.bitnol.com 
* API URI: http://api.gitnol.com 
* Description: Modular demo of the application of Cipher API 
* License: MIT License 
*/ 

// Video with cipher signature 
$video_id = "zDrNLZ1uJ2w"; 
$results = getYTLink($video_id); 
var_dump($results); 

function getYTLink($video_id){ 

    // get_video_info url formation 
    // although for cipher signature we have to get the details from the video's webapge not from get_video_info object 
    $info_url = "http://www.youtube.com/get_video_info?el=detailpage&asv=3&video_id=".$video_id; 

    // youtube webpage url formation 
    $yt_url = 'http://www.youtube.com/watch?v='.$video_id.'&gl=US&persist_gl=1&hl=en&persist_hl=1';; 

    // get the contents from the url 
    $raw_data = file_get_contents($info_url); 

    // parse the data received and save it as an array 
    $output = array(); 
    parse_str($raw_data,$output); 

    // check the status of the get_video_info object 
    if($output['status']=='ok'){ 

     // check for the cipher signature 
     $cipher = (isset($output['use_cipher_signature']) && $output['use_cipher_signature']=='True') ? true : false; 

     // If cipher is true then we have to decode it 
     if($cipher == true){ 

      // if cipher is true then we have to change the plan and get the details from the video's youtube wbe page 
      $yt_html = file_get_contents($yt_url); 

      // parse for the script containing the configuration 
      preg_match('/ytplayer.config = {(.*?)};/',$yt_html,$match); 
      $yt_object = @json_decode('{'.$match[1].'}') ; 

      /// check if we are able to parse data 
      if(!is_object($yt_object)){ 
       //'Sorry! Unable to parse Data'; 
       return 'Error Code 1'; 
      }else{ 

       // parse available formats 
       $formats = $yt_object->args->url_encoded_fmt_stream_map; 
       // get the player id from assets section 
       $player_id = strbtwn($yt_object->assets->js,'html5player-','.js'); 
       $player_id = explode("/", $player_id); 
       $player_id = $player_id[0]; 

       // get the algo dictionary 
       // first check if the file exists 
       if(file_exists('./algo.json')) 
        $algos = json_decode(file_get_contents('algo.json'),true); 
       else{ 
        // API call to fetch the algo dictionary 
        $algos_dict = file_get_contents("http://api.gitnol.com/getAlgo.php?playerID=".$player_id); 

        // saving the algo dictonary in local env for easy access 
        // Note: Developers should save the dictionary in their local env. 
        // Only make the API call for the new player ids which is not present in the algo dictionary. 
        // Repeated violation will results in IP ban. 
        file_put_contents('algo.json', $algos_dict); 

        $algos = json_decode($algos_dict,true); 
       } 

       /// check if the algo exist for the given player id 
       if(!array_key_exists($player_id, $algos)){ 

        // if the algo dictionary is old then fetch a new one 
        $algos_dict = file_get_contents("http://api.gitnol.com/getAlgo.php?playerID=".$player_id); 
        file_put_contents('algo.json', $algos_dict); 

        $algos = json_decode($algos_dict,true); 
        $algo = $algos[$player_id][1]; 

       }else{ 
        $algo = $algos[$player_id][1]; 
       } 

       // download links formation 
       $dlinks = array(); 
       $links = explode(',',$formats); 


       foreach ($links as $link) { 
        parse_str($link,$linkarr); 

        // parse link array one by one and decrypt the signature 
        $dlinks[$linkarr['itag']] = $linkarr['url'] . "&signature=" . decrypt($linkarr['s'],$algo); 
       } 
       return $dlinks; 
      } 

     }else{ 
      //'Video Is not cipher not needed to decode'; 
      return 'Error Code 2'; 
     } 
    }else{ 
     //'Unable to get Video Info'; 
     return 'Error Code 3'; 
    } 
} 
// string helper function 
function strbtwn($content,$start,$end){ 
    $r = explode($start, $content); 
    if (isset($r[1])){ 
     $r = explode($end, $r[1]); 
     return $r[0]; 
    } 
    return ''; 
} 

// signature decoding 
// parse the python string operation into php string operation 
function decrypt($sig,$algo){ 
    $funcarr = explode(' + ', $algo); 
    $decrypt = ''; 
    foreach($funcarr as $singfunc){ 
     $singfunc = substr($singfunc,2,-1); 
     $operators = explode(':', $singfunc); 
     if (sizeof($operators) == 1) { 
      $decrypt .= $sig[$operators[0]]; 
     } 
     if (sizeof($operators) == 2) { 
      if($operators[0] == ''){ 
       $decrypt .= substr($sig, 0 ,$operators[1]); 
      } 
      if($operators[1] == ''){ 
       $decrypt .= substr($sig, $operators[0]); 
      } 
      if($operators[0] >= 0 && $operators[1] >= 0){ 
       $decrypt .= substr($sig, $operators[0], $operators[1] - $operators[0]); 
      } 
     } 
     if (sizeof($operators) == 3) { 
      if($operators[0] == '' && $operators[1] == ''){ 
       $decrypt .= strrev($sig); 
      } 
      if($operators[0] >=0 && $operators[1] == ''){ 
       $decrypt .= strrev(substr($sig, 0, $operators[0] + 1)); 
      } 
      if($operators[0] >=0 && $operators[1] >= 0){ 
       $decrypt .= strrev(substr($sig, $operators[1] + 1, $operators[0] - $operators[1])); 
      } 
     } 
    } 
    return $decrypt; 
} 
?> 

我认为这将解决您的问题。

+0

谢谢你Akhlish这使得它非常简单:) – user3676425 2014-10-20 15:48:29