【发布时间】:2017-08-20 22:03:05
【问题描述】:
我正在安装这个链接:youtube-mp3.org download class
youtubetomp3.php //类 php
<?php
class YoutubeToMP3 {
const DOWNLOAD = 1;
const LINK = 2;
/**
* Obtem o link de download do MP3 de um video do YouTube
* @param string $url URL do video no YouTube
* @param integer $action Acão que será executada com o link (YoutubeToMP3::DOWNLOAD, YoutubeToMP3::LINK)
* @return mixed Quando o parametro $action for definido como YoutubeToMP3::DOWNLOAD redireciona
* para o download do arquivo, quando YoutubeToMP3::LINK traz o link como retorno
*/
public static function get($url, $action = self::LINK) {
$currentTime = time();
$videoId = self::getYoutubeId($url);
$itemInfoUrl = "http://www.youtube-mp3.org/a/itemInfo/?video_id={$videoId}&ac=www&t=grp&r={$currentTime}";
$itemInfo = self::httpRequest($itemInfoUrl);
$sequence = $videoId . $currentTime;
$requestId = $itemInfo['h'];
$cc = self::cc($sequence);
$MP3URL = "http://www.youtube-mp3.org/get?ab=128&video_id={$videoId}&h={$requestId}&r={$currentTime}.{$cc}";
if ($action == self::DOWNLOAD):
self::redirect($MP3URL);
else:
return $MP3URL;
endif;
}
/**
* Realiza uma requisição do tipo GET para uma url dada
* @param string $url Url que será requisitada
* @return array Array associativo do JSON retornado
*/
private static function httpRequest ($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
// Clear response string
$jsonString = str_replace(array('info = ', ';'), '', $result);
$parsedJSON = json_decode($jsonString, true);
return $parsedJSON;
}
/**
* Obtem o ID do um video do YouTube a partir de uma URL
* @param string $url URL do video
* @return mixed String com o id caso seja uma URL válida ou false do contrário
*/
private static function getYoutubeId ($url) {
$pattern = '%^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|.*v=))([\w-]{10,12})($|&).*$%x';
$result = preg_match($pattern, $url, $matches);
if ($result !== false):
return $matches[1];
endif;
return false;
}
/**
* Gera uma sequencia necessária para a API do youtube-mp3.org
* @param string $a Id do video concatenado com o timestamp atual
* @return string Sequencia
*/
private static function cc ($a) {
$AM = 65521;
$c = 1;
$b = 0;
$d;
$e;
for ($e = 0; $e < strlen($a); $e++):
$d = self::charCodeAt($a,$e);
$c = ($c + $d) % $AM;
$b = ($b + $c) % $AM;
endfor;
return $b << 16 | $c;
}
/**
* Metodo utilitário
*/
private static function charCodeAt($str, $i){
return ord(substr($str, $i, 1));
}
/**
* Header Redirect
*
* Header redirect in two flavors
* For very fine grained control over headers, you could use the Output
* Library's set_header() function.
*
* @param string $uri URL
* @param string $method Redirect method 'auto', 'location' or 'refresh'
* @param int $code HTTP Response status code
* @return void
*/
private static function redirect ($uri = '', $method = 'auto', $code = NULL) {
if (!preg_match('#^(\w+:)?//#i', $uri)) {
$uri = site_url($uri);
}
if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE) {
$method = 'refresh';
} elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code))) {
if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1') {
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET')? 303 : 307;
} else {
$code = 302;
}
}
switch ($method) {
case 'refresh':
header('Refresh:0;url='.$uri);
break;
default:
header('Location: '.$uri, TRUE, $code);
break;
}
exit;
}
}
?>
//使用php
<?php
include ('youtubetomp3.php');
echo $link = YoutubeToMP3::get('http://www.youtube.com/watch?v=tGa5-nojscY', YoutubeToMP3::DOWNLOAD);
?>
可能是这些问题;
a-) 是因为 youtube-mp3.org 还是我的托管?
b-) 是不是因为 $currentTime = time(); ?
c-) 是不是因为 $requestId = $itemInfo['h']; ?
【问题讨论】:
-
有什么问题?你得到什么错误(检查日志!)?你希望它做什么?
-
可能是您的主机中没有启用或安装 curl,这就是您无法使用它的原因,您应该检查日志以查看错误。
-
你的 localhost 和使用 phpinfo() 托管有什么区别;
-
@Burak, @Bart Friederichs (error dosyasında bunlar yazıyor) 这是我的错误日志:
PHP Warning: curl_setopt() [<a href='function.curl-setopt'>function.curl-setopt</a>]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/***/public_html/***.com/youtubetomp3.php on line 44
标签: php hash time localhost hosting