效果
protected function fetchUrl($url,$userAgent = '')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
if (!empty(userAgent)) {
curl_setopt($curl, CURLOPT_USERAGENT, userAgent);
}
$content = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return ['code' => $code, 'content' => $content];
}
BiLiBiLi
$response = $this->fetchUrl($url);
if (200 != $response['code']) {
throw new ParseException('擷取BiLiBiLi視訊資訊失敗!');
}
$matched = preg_match('/cid=(.*?)&aid=(.*?)&attribute=(.*?)&bvid=(.*?)&show_bv/', $response['content'], $matches); //解析播放重要參數
if (empty($matches)) {
throw new ParseException('解析BiLiBiLi視訊ID失敗');
}
$titleMatched = preg_match('/<h1 title="(.*?)" class="video-title">/', $response['content'], $titleMatches);
//解析标題
if (empty($titleMatches)) {
$title = '';
} else {
$title = $titleMatches[1]; // 視訊标題
}
//iframe 播放位址
$playUrl = "https://player.bilibili.com/player.html?aid={$matches[2]}&bvid={$matches[4]}&cid={$matches[1]}&page=1";
愛奇藝
$response = $this->fetchUrl($url);
if (200 != $response['code']) {
throw new ParseException('擷取愛奇藝視訊資訊失敗!');
}
$matched = preg_match('/page-info=\'(.*?)\' :video-info=/', $response['content'], $matches); //解析視訊參數json
if (empty($matches)) {
throw new ParseException('解析愛奇藝視訊ID失敗');
}
$data = json_decode($matches[1], true);
$title = $data['tvName'];
$playUrl = "https://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid={$data['vid']}&tvId={$data['tvId']}&accessToken=2.ef9c39d6c7f1d5b44768e38e5243157d&appKey=8c634248790d4343bcae1f66129c1010&appId=1368&height=100%&width=100%";