php通过输入一个url获取优酷、土豆、酷6、搜狐、新浪视频地址
class Video { /** * 通过视频网站url获取swf地址 * @param string $content * @return string */ public function getVideo($url) { $urls = parse_url($url); $host = strtolower(trim($urls["host"])); $vhconfs = array( 'youku.com' => array('p' => '/\/id_([\w\d\=]+)\.html/', 'c' => 'utf-8', 'tp' => '/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i', 'ip' => '~(?:(?:\_href\s*\=\s*[\'\']iku\:\/\/.+?)|(?:\&pic\s*\=\s*)|(?:\&screenshot\s*\=\s*))(http\:\/\/[\w\d]+\.ykimg\.com\/[^\|\'\'\s]+)~i', 'ppp' => 1), 'sina.com.cn' => array('p' => '/\/(\d+)\-(\d+)\.html/', 'c' => 'utf-8', 'tp' => '/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/', 'ip' => '~pic\s*[\:\=]\s*[\'\'](http\:\/\/[^\']+?\.(?:jpg|jpeg|gif|png|bmp))[\'\']~i', 'ppp' => 1, 'gzip' => 1), 'tudou.com' => array('p' => '/view\/([\w\d\-\_]+)\/' . '*$/', 'c' => 'gbk', 'tp' => '/\<title\>(.+?)(?:[\-\_].*?)?\<\/title\>/', 'ip' => '/(?:bigItemUrl|pic)\s*[\=\:]\s*[\'\']([^\'\']+?)[\'\']/i', 'ppp' => 1, 'gzip' => 1), 'ku6.com' => array('p' => '/\/([\w\-\_\.]+)\.html/', 'c' => 'gbk', 'tp' => '/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i', 'ip' => '~(?:(?:\<span class=\'s_pic\'\>)|(?:cover\s*[\:\=]\s*[\'\']))(http\:\/\/[^\'\<]+?\.(?:jpg|jpeg|gif|png|bmp))~i', 'ppp' => 0), 'sohu.com' => array('p' => '/\/([\d]+)\/?$/', 'c' => 'gbk', 'tp' => '/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i', 'ip' => '/(?:(?:[\'\']og\:image[\'\']\s*content)|(?:cover))\s*[\:\=]\s*[\'\']([^\'\']+?)[\'\']/', 'ppp' => 1, 'gzip' => 1), ); $swf = array( 'youku.com' => 'http://player.youku.com/player.php/sid/FLASHVAR=/v.swf', 'ku6.com' => 'http://player.ku6.com/refer/FLASHVAR/v.swf', 'sina.com.cn' => 'http://vhead.blog.sina.com.cn/player/outer_player.swf?vid=FLASHVAR', 'sohu.com' => 'http://v.blog.sohu.com/fo/v4/FLASHVAR', 'tv.sohu.com' => 'http://share.vrs.sohu.com/FLASHVAR/v.swf', 'tudou.com' => 'http://www.tudou.com/v/FLASHVAR', ); foreach ($vhconfs as $k => $v) { if (false !== strpos($host, $k)) { $return = array(); if (preg_match($v["p"], $url, $m) && $m[1]) { $return["id"] = $m[1]; $return["host"] = $k; $return["url"] = $url; } elseif ($v["ppp"]) { if ("tudou.com" == $k) { if ( (false !== strpos($url, "play/")) || (false !== strpos($url, "programs/")) ) { $html = $this->dfopen($url); if ($v["gzip"]) { $html = $this->gzdecode($html); } $iid = ""; $icode = ""; if (preg_match('~(?:(?:[\?\&\#]iid\=)|(?:\d+i))(\d+)~', $url, $m) && $m[1]) { $iid = $m[1]; } elseif (preg_match('~(?:(?:\,iid\s*=)|(?:\,defaultIid\s*=)|(?:\.href\)\s*\|\|))\s*(\d+)~', $html, $m) && $m[1]) { $iid = $m[1]; } if (preg_match("~" . $iid . ".*?icode\s*[\:\=]\s*(?:[^\'\"]*?)[\'\"]([\w\d\-\_]+)[\'\"]~s", $html, $m) && $m[1]) { $icode = $m[1]; } if ($icode) { $return["id"] = $icode; $return["url"] = $url; } } } elseif ("youku.com" == $k) { if (preg_match("~\/v\_playlist\/.+?\.htm~", $url) && ($html = $this->dfopen($url))) { if ($v["gzip"]) { $html = $this->gzdecode($html); } $id = ""; if (preg_match("~\_href\s*\=\s*[\'\"]iku\:\/\/.+?http\:\/\/v\.youku\.com\/v\_show\/id\_([\w\d]+)\.htm~i", $html, $m) && $m[1]) { $id = $m[1]; } if ($id) { $return["id"] = $id; $return["url"] = $url; } } } elseif ("sina.com.cn" == $k) { if (preg_match("/video\.sina\.com\.cn\/.+?\/([\d\-]+)\.html/", $url) && ($html = $this->dfopen($url))) { if ($v["gzip"]) { $html = $this->gzdecode($html); } $id = ""; if (preg_match("~vid\s*[\:]\s*[\'\"]([^\'\"]+?)[\'\"]~i", $html, $m) && $m[1]) { $id = $m[1]; } if ($id) { $return["id"] = $id; $return["url"] = $url; } } } elseif ("sohu.com" == $k) { if (preg_match("~tv\.sohu\.com\/([\w]+\/)+[\w]+\.shtml~i", $url) && ($html = $this->dfopen($url))) { if ($v["gzip"]) { $html = $this->gzdecode($html); } $id = ""; if (preg_match("~vid\s*[\:\=]\s*[\'\"]([^\'\"]+?)[\'\"]~i", $html, $m) && $m[1]) { $id = $m[1]; } if ($id) { $return["id"] = $id; $return["url"] = $url; $return["host"] = "tv.sohu.com"; } } } if ($return && !isset($return["host"])) { $return["host"] = $k; } } } } if( !empty($return) ) { $return['swf'] = str_replace('FLASHVAR', $return['id'], $swf[$return['host']]); return $return['swf']; } else { return FALSE; } } public function gzdecode($data) { $flags = ord(substr($data, 3, 1)); $headerlen = 10; $extralen = 0; $filenamelen = 0; if ($flags & 4) { $extralen = unpack('v', substr($data, 10, 2)); $extralen = $extralen[1]; $headerlen += 2 + $extralen; } if ($flags & 8) $headerlen = @strpos($data, chr(0), $headerlen) + 1; if ($flags & 16) $headerlen = @strpos($data, chr(0), $headerlen) + 1; if ($flags & 2) $headerlen += 2; $unpacked = @gzinflate(substr($data, $headerlen)); if ($unpacked === FALSE) $unpacked = $data; return $unpacked; } public function dfopen($url, $limit = 10485760, $post = '', $cookie = '', $bysocket = false, $timeout = 5, $agent = "") { if (ini_get('allow_url_fopen') && !$bysocket && !$post) { $fp = @fopen($url, 'r'); $s = $t = ''; if ($fp) { while ($t = @fread($fp, 2048)) { $s.=$t; } fclose($fp); } if ($s) { return $s; } } $return = ''; $agent = $agent ? $agent : "Mozilla/5.0 (compatible; Googlebot/2.1; +http:/" . "/www.google.com/bot.html)"; $matches = parse_url($url); $host = $matches['host']; $script = $matches['path'] . (isset($matches['query']) ? '?' . $matches['query'] : '') . (isset($matches['fragment']) ? '#' . $matches['fragment'] : ''); $script = $script ? $script : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if ($post) { $out = "POST $script HTTP/1.1\r\n"; $out .= "Accept: */" . "*\r\n"; $out .= "Referer: $url\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "Accept-Encoding: none\r\n"; $out .= "User-Agent: $agent\r\n"; $out .= "Host: $host\r\n"; $out .= 'Content-Length: ' . strlen($post) . "\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cache-Control: no-cache\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; $out .= $post; } else { $out = "GET $script HTTP/1.1\r\n"; $out .= "Accept: */" . "*\r\n"; $out .= "Referer: $url\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Accept-Encoding: none\r\n"; $out .= "User-Agent: $agent\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; } $fp = $this->jfsockopen($host, $port); if (!$fp) { return false; } else { fwrite($fp, $out); $return = ''; while (!feof($fp) && $limit > -1) { $limit -= 8192; $return .= @fread($fp, 8192); if (!isset($status)) { preg_match('|^HTTP/[^\s]*\s(.*?)\s|', $return, $status); $status = $status[1]; if ($status != 200) { return false; } } } fclose($fp); preg_match("/^Location: ([^\r\n]+)/m", $return, $match); if (!empty($match[1]) && $location = $match[1]) { if (strpos($location, ":/" . "/") === false) { $location = dirname($url) . '/' . $location; } $args = func_get_args(); $args[0] = $location; return call_user_func_array("dfopen", $args); } if (false !== ($strpos = strpos($return, "\r\n\r\n"))) { $return = substr($return, $strpos); $return = preg_replace('~^\r\n\r\n(?:[\w\d]{1,8}\r\n)?~', "", $return); if ("\r\n\r\n" == substr($return, -4)) { $return = preg_replace('~(?:\r\n[\w\d]{1,8})?\r\n\r\n$~', "", $return); } } return $return; } } public function jfsockopen($hostname, $port, $errno = null, $errstr = null, $timeout = 30) { $fp = false; if (function_exists('fsockopen')) { @$fp = fsockopen($hostname, $port, $errno, $errstr, $timeout); } elseif (function_exists('pfsockopen')) { @$fp = pfsockopen($hostname, $port, $errno, $errstr, $timeout); } return $fp; } }