php调用新浪接口将长链接转为短链接
- <!--生成短连接-->
- <?php
- /**
- * 调用新浪接口将长链接转为短链接
- * @param string $source 申请应用的AppKey
- * @param array|string $url_long 长链接,支持多个转换(需要先执行urlencode)
- * @return array
- */
- function getSinaShortUrl($source, $url_long)
- {
-
- // 参数检查
- if (empty($source) || !$url_long) {
- return false;
- }
-
- // 参数处理,字符串转为数组
- if (!is_array($url_long)) {
- $url_long = array($url_long);
- }
-
- // 拼接url_long参数请求格式
- $url_param = array_map(function ($value) {
- return '&url_long=' . urlencode($value);
- }, $url_long);
-
- $url_param = implode('', $url_param);
-
- // 新浪生成短链接接口
- $api = 'http://api.t.sina.com.cn/short_url/shorten.json';
-
- // 请求url
- $request_url = sprintf($api . '?source=%s%s', $source, $url_param);
-
- $result = array();
-
- // 执行请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_URL, $request_url);
- $data = curl_exec($ch);
- if ($error = curl_errno($ch)) {
- return false;
- }
- curl_close($ch);
-
- $result = json_decode($data, true);
-
- return $result;
- }
-
- // AppKey
- $source = '31641035';
-
- // 单个链接转换
- $url_long = 'https://url';//地址
- //var_dump($url_long);
- $data = getSinaShortUrl($source, $url_long);
-
- ?>
-
-
- <!--生成二维码-->
- <?php
- include '../code/phpqrcode.php';
- //二维码内容
- $value = '';//地址
- //容错级别
- $errorCorrectionLevel = 'L';
- //生成图片大小
- $matrixPointSize = 5;
- //生成二维码图片
- QRcode::png($value, '../../img/code/qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
- //$logo = '../../img/logo/1.png';//准备好的logo图片
- $logo = '../../img/logo/' . $shlogo;
-
- //var_dump($logo);
-
- $QR = '../../img/code/qrcode.png'; //已经生成的原始二维码图
- if ($logo !== FALSE) {
- $QR = imagecreatefromstring(file_get_contents($QR));
- $logo = imagecreatefromstring(file_get_contents($logo));
- $QR_width = imagesx($QR); //二维码图片宽度
- $QR_height = imagesy($QR); //二维码图片高度
- $logo_width = imagesx($logo); //logo图片宽度
- $logo_height = imagesy($logo); //logo图片高度
- $logo_qr_width = $QR_width / 4;
- $scale = $logo_width / $logo_qr_width;
- $logo_qr_height = $logo_height / $scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
- //重新组合图片并调整大小
- imagecopyresampled(
- $QR,
- $logo,
- $from_width,
- $from_width,
- 0,
- 0,
- $logo_qr_width,
- $logo_qr_height,
- $logo_width,
- $logo_height
- );
- }
- //输出图片
- imagepng($QR, '../../img/code/code_img.png');
- //echo '<img src="code_img.png">';
- ?>
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。