PHP第三方短网址聚合API实例

集合了微信和腾讯的短连接

说明

调用方式:XXX.php?type=1&url=地址

1=腾讯

2=微信

案例

<?php
header('Content-type:text/html; Charset=utf-8');
!empty($_REQUEST['url']) ? $url = $_REQUEST['url'] : exit(json_encode(['code'=>202,"msg"=>"缺少参数"],JSON_UNESCAPED_UNICODE));
!empty($_REQUEST['type']) ? $type = $_REQUEST['type'] : exit(json_encode(['code'=>202,"msg"=>"缺少参数"],JSON_UNESCAPED_UNICODE));
$Service = new ClassService();
$api = $Service->getgrab($type,$url);
echo json_encode($api,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);

class ClassService{
	//抓取
	public function getgrab($api,$url) {
		switch ($api){
			case "1": $api = "https://umapi.top/api/txurl.php?url=";break;
			case "2": $api = "https://api.oioweb.cn/api/dwz.php?type=1&url=";break;
			default: die('{"code": 202,"msg": "参数不存在"}');
		}
		$data = $this->myCurl($api.$url);
		$json = json_decode($data,true);
		if($json['code'] == 200){
			$value = array('code'=>200,'msg'=>'成功','url'=>$json['url']);
		}else if($json['code'] == 1){
			$value = array('code'=>200,'msg'=>'成功','url'=>$json['url']['url_short']);
		}else{
			$value = array('code'=>202,'msg'=>'失败');
		}
		return $value;
	}
	//Curl GET
	public function myCurl($url){
		$ch = curl_init();// Curl 初始化  		  		
		$ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'; // 伪造抓取 UA  		
		$ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
		curl_setopt($ch, CURLOPT_URL, $url); // 设置 Curl 目标  		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Curl 请求有返回的值  		
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // 设置抓取超时时间  		
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 跟踪重定向  
		curl_setopt($ch, CURLOPT_REFERER, $url); // 伪造来源网址  
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip)); //伪造IP  
		curl_setopt($ch, CURLOPT_USERAGENT, $ua); // 伪造ua   
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不检查 证书中 SSL 加密算法是否存在
		curl_setopt($ch, CURLOPT_AUTOREFERER, true); ////当Location:重定向时,自动设置header中的Referer:信息
		curl_setopt($ch, CURLOPT_ENCODING, ''); //解决网页乱码问题
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0
		curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
		$content = curl_exec($ch);
		curl_close($ch); // 结束 Curl  
		return $content; // 函数返回内容
	}
}