已获得查看权限
<?php
class CWaterMask {
static public function analyzing($url) {
preg_match('/([\w-]+\.)+\w+(\:\d{2,6})?/', $url, $domain);
switch ($domain[0]) {
case '':
return self::result(500, '请传入解析url参数,例:https://www.360mb.net/?url=https://v.douyin.com/ehHpu7V/');
break;
case 'v.douyin.com':
return self::douyin($url);
break;
case 'v.kuaishou.com':
return self::kuaishou($url);
break;
default:
return self::result(500, '抱歉,此url暂不支持!');
}
}
static public function douyin($url) {
$url = self::httpRequest($url, 'GET');
preg_match('/(?<=video\/)\d*(?=\/\?region)/', $url['location'], $matches);
$item_ids = $matches[0];
$vidoUrl = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' . $item_ids;
$result = self::httpRequest($vidoUrl, 'GET');
$vid = $result['response']['item_list'][0]['video']['play_addr']['uri'];
if (isset($vid)) {
$video_url = 'https://aweme.snssdk.com/aweme/v1/play/?video_id=' . $vid . '&ratio=720p&line=0';
$music = $result['response']['item_list'][0]['music']['play_url']['uri'];
$nickname = $result['response']['item_list'][0]['author']['nickname'];
$return = array('nickname' => $nickname, 'video_url' => $video_url, 'music' => $music);
return self::result(200, $return);
} else {
return self::result(500, '解析出错!');
}
}
static public function kuaishou($url) {
$vurl = self::httpRequest($url, 'GET');
preg_match('/(?<=video\/)\w*(?=\?fid)/', $vurl['location'], $matches);
$str_qury1 = 'query visionVideoDetail($photoId: String, $type: String, $page: String, $webPageArea: String)';
$str_qury2 = '(photoId: $photoId, type: $type, page: $page, webPageArea: $webPageArea)';
$query = array("operationName" => "visionVideoDetail", "variables" => array('photoId' => $matches[0], "page" => "detail"), "query" => $str_qury1 . "{\n visionVideoDetail" . $str_qury2 . "{\n photo {\n id\n duration\n caption\n likeCount\n realLikeCount\n coverUrl\n photoUrl\n }\n}\n}\n",);
$query = json_encode($query, 320);
$headers = array('content-type: application/json', 'Referer:' . $vurl['location']);
$result = self::httpRequest('https://video.kuaishou.com/graphql', 'POST', $query, $headers);
if (isset($result['response']['data']['visionVideoDetail']['photo']['photoUrl'])) {
return self::result(200, $result['response']['data']['visionVideoDetail']['photo']);
} else {
return self::result(500, '解析出错!');
}
}
static public function httpRequest($url, $method = 'POST', $postfields = null, $headers = array()) {
$method = strtoupper($method);
$ci = curl_init();
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
switch ($method) {
case "POST":
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
}
break;
default:
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);
break;
}
$ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
curl_setopt($ci, CURLOPT_URL, $url);
if ($ssl) {
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ci, CURLOPT_MAXREDIRS, 2);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = json_decode(curl_exec($ci), true);
$requestinfo = curl_getinfo($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$location = curl_getinfo($ci, CURLINFO_EFFECTIVE_URL);
curl_close($ci);
return array('location' => $location, 'response' => $response, 'requestinfo' => $requestinfo);
}
static public function result($errno = 0, $data = '') {
header("Content-type: application/json;charset=utf-8");
$errno = intval($errno);
$result = array('code' => $errno, 'message' => $data);
return json_encode($result, 320);
}
}
评论(0)