protected static function get_content_length($curlopts) { $gid = 'content_length_' . md5(serialize($curlopts)); $data = ''; if ($data = \tools\cache::memcached()->get($gid)) { return $data; } $ch = curl_init(); if (!isset($curlopts[CURLOPT_HTTPHEADER])) { $curlopts[CURLOPT_HTTPHEADER] = array(); } $curlopts[CURLOPT_HTTPHEADER] = array_merge($curlopts[CURLOPT_HTTPHEADER], array('Connection: close')); $curlopts[CURLOPT_CUSTOMREQUEST] = 'HEAD'; $curlopts[CURLOPT_NOBODY] = true; $curlopts[CURLOPT_HEADER] = true; foreach ($curlopts as $curlopt_k => $curlopt_v) { curl_setopt($ch, $curlopt_k, $curlopt_v); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //~ curl_setopt($ch,CURLOPT_TIMEOUT,3); $data = curl_exec($ch); curl_close($ch); $matches = array(); preg_match('#Content-Length:[^0-9\\r\\n](\\d+)#i', $data, $matches); if (empty($matches[1])) { throw new \Exception('Unable to get filesize!'); } $data = '' . $matches[1]; \tools\cache::memcached()->set($gid, $data); return $data; }
/** * * @params,array('vid','name') * */ public function get_m3u8($params) { if (empty($params['vid'])) { throw new \Exception('Please input vid.'); } $gid = $this->PLUGIN_NAME . '_' . $params['vid'] . '_m3u8'; $data = ''; if ($data = \tools\cache::memcached()->get($gid)) { return $data; } $API = 'http://play.youku.com/play/get.json?vid=' . $params['vid'] . '&ct=12'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $API); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Referer: http://v.youku.com/v_show/' . $params['vid'] . '.html?x')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $data = curl_exec($ch); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); curl_close($ch); $header = explode("\r\n", substr($data, 0, $headerSize)); $retval = substr($data, $headerSize); foreach ($header as $headeritem) { $headerdetail = explode(": ", $headeritem); if (!empty($headerdetail[1])) { $headerarray[$headerdetail[0]] = $headerdetail[1]; } else { $headerarray[$headerdetail[0]] = ''; } } $cookies = explode('; ', $headerarray['Set-Cookie']); $ret['cookie'] = $cookies[0]; $r_key = substr($cookies[0], 3, -1); if (!empty($retval)) { $rs = json_decode($retval, true); $ep = $rs['data']['security']['encrypt_string']; if (!empty($ep)) { $ip = $rs['data']['security']['ip']; $videoid = $rs['data']['id']; list($sid, $token) = explode('_', self::yk_e('becaf9be', base64_decode($ep))); $ep = urlencode(base64_encode(self::yk_e('bf7e5f01', $sid . '_' . $videoid . '_' . $token))); $ret['url'] = $final_url = 'http://pl.youku.com/playlist/m3u8?ctype=12&ep=' . $ep . '&ev=1&keyframe=1&oip=' . $ip . '&sid=' . $sid . '&token=' . $token . '&vid=' . $videoid . '&type=mp4'; } else { throw new \Exception('Invalid vid.'); } } else { throw new \Exception('An error occurred on fetch data.'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ret['url']); curl_setopt($ch, CURLOPT_COOKIE, $ret['cookie']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $data = curl_exec($ch); curl_close($ch); \tools\cache::memcached()->set($gid, $data, 3600 * 4); return $data; }