<?php
/**
* 抖音链接url
*/
private function douyin ($url){
$number_maps = [
0 => ['', '', ''],
1 => ['', '', ''],
2 => ['', '', ''],
3 => ['', '', ''],
4 => ['', '', ''],
5 => ['', '', ''],
6 => ['', '', ''],
7 => ['', '', ''],
8 => ['', '', ''],
9 => ['', '', ''],
];
$opts = [
'http' => [
'header' => "User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36",
]
];
$context = stream_context_create($opts);
$html = file_get_contents($url, false, $context);
if (!$html) {
return [];
}
$data = [];
// 封面图
preg_match('/id="videoPoster"\s*style="background-image:url\((.*?)\)">/i', $html, $match);
$data['cover_url'] = $match[1];
//抖音昵称
preg_match('/class="user-info-name">\@(.*?)<\//i', $html, $match);
$data['douyin_name'] = $match[1];
//抖音ID
preg_match('/<\s*p\s*class="user-info-id">.*?ID:(.*?)<\/p>/i', $html, $match);
$data['douyin_uid'] = str_replace(' ', '', strip_tags($match[1]));
//点赞数
preg_match('/<\s*div\s*class="info-item info-like"\s*.*?>.*?<\s*p\s*class="count"\s*>(.*?)<\/p>/i', $html, $match);
$data['thumb_times'] = str_replace(' ', '', strip_tags($match[1]));
// 评论数
preg_match('/<\s*div.*data-item="comment"\s*>.*?<\s*p\s*class="count"\s*>(.*?)<\/p>/i', $html, $match);
$data['comment_times'] = str_replace(' ', '', strip_tags($match[1]));
// $data_str = json_encode($data);
foreach ($number_maps as $k => $v) {
$data = str_replace($v, $k, $data);
}
$thumb_times_unit = strtolower(substr($data['thumb_times'], -1));
if ($thumb_times_unit == 'w') {
$data['thumb_times'] = intval(floatval($data['thumb_times']) * 10000);
} else if ($thumb_times_unit == 'k') {
$data['thumb_times'] = intval(floatval($data['thumb_times']) * 1000);
}
$comment_times_unit = strtolower(substr($data['comment_times'], -1));
if ($comment_times_unit == 'w') {
$data['comment_times'] = intval(floatval($data['comment_times']) * 10000);
} else if ($comment_times_unit == 'k') {
$data['comment_times'] = intval(floatval($data['comment_times']) * 1000);
}
preg_match('/itemId\s*:\s*"(\d+)"/i', $html, $match);
$data['douyin_item_id'] = $match[1];
preg_match('/uid:\s*"(\d+)"/i', $html, $match);
$user_id = $match[1];
$html = file_get_contents('http://www.iesdouyin.com/share/user/' . $user_id, false, $context);
$data['user_id'] = $user_id;
//粉丝数
preg_match('/<\s*span\s*class="follower block"\s*.*?>.*?<\s*span\s*class="num"\s*.*?>(.*?)<\/span>/i', $html, $match);
$data['fans_times'] = str_replace(' ', '', strip_tags($match[1]));
//关注数
preg_match('/<\s*span\s*class="liked-num block"\s*.*?>.*?<\s*span\s*class="num"\s*.*?>(.*?)<\/span>/i', $html, $match);
$data['like_times'] = str_replace(' ', '', strip_tags($match[1]));
foreach ($number_maps as $k => $v) {
$data = str_replace($v, $k, $data);
}
$fans_times_unit = strtolower(substr($data['fans_times'], -1));
if ($fans_times_unit == 'w') {
$data['fans_times'] = intval(floatval($data['fans_times']) * 10000);
} else if ($fans_times_unit == 'k') {
$data['fans_times'] = intval(floatval($data['fans_times']) * 1000);
}
$like_times_unit = strtolower(substr($data['like_times'], -1));
if ($like_times_unit == 'w') {
$data['like_times'] = intval(floatval($data['like_times']) * 10000);
} else if ($like_times_unit == 'k') {
$data['like_times'] = intval(floatval($data['like_times']) * 1000);
}
return $data;
}