应用场景:
- 语言:php;
- 利用淘宝的api,判别用户所在国家;
- 相同的视频上传在youtube和Blibili,以iframe嵌入页面;
- 如果是中国,播放b站视频;
- 如果非中国地区,播放Youtube视频。
变量:
$vcodecn 为b站视频flash URL;
$vcode 为Youtube视频flash URL;
$iframe 为iframe代码。
视频播放页面
在需要嵌入视频处插入:
<?php echo"$iframe";?>
IP地址判断
<?php
// 淘宝API查询国家代码
$url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip();
$json = json_decode(file_get_contents($url));
$country = $json->{"data"}->{"country_id"};
// 判断国家代码 把需要判断的国家加在下面就行了
$countrys = array("CN",);
if (in_array($country, $countrys))
{
$iframe = "<embed quality=’high’ allowfullscreen=’true’ type=’application/x-shockwave-flash’ pluginspage=’http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash’ src=’".$vcodecn."’ ></embed>";
}
else
{
$iframe = "<iframe class=’embed-responsive-item’ src=’".$vcode."’> </iframe>";
}
// 获取IP地址
function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if (isset($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {
$arr = explode(‘,’, $_SERVER[‘HTTP_X_FORWARDED_FOR’]);
$pos = array_search(‘unknown’,$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER[‘HTTP_CLIENT_IP’])) {
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
}elseif (isset($_SERVER[‘REMOTE_ADDR’])) {
$ip = $_SERVER[‘REMOTE_ADDR’];
}
// IP地址合法验证
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array(‘0.0.0.0’, 0);
return $ip[$type];
}
?>