PHP实现百度、新浪的API接口调用生成短连接网址

在实际过程当中咱们会有这样的场景,就是发送短信的时候,里面须要嵌入咱们的网址,但是网址都是很长的。php

若是你通常都是在手机上操做的话,能够在微信小程序中搜索:短连接的生成html

或者在文章底部扫描小程序二维码进行使用laravel

可是短信内容,最多只能七十个字左右,多余就会出现发送两条的状况,然而这并非咱们想要的。

因此,基于这种需求咱们急需将长连接转为短连接。经常使用的能够看到新浪微博的分享地址。下面来看,如何实现?复制代码

新浪提供了长连接转为短连接的API,能够把长连接转为 t.cn/xxx 这种格式的短连接。git

(t.cn新浪已中止服务,暂时用不了,如想使用请联系新浪官方)github

百度提供了长连接转为短连接的API,能够把长连接转为 dwz.cn/xxx 这种格式的短连接。数据库

百度老接口将于 近期 中止服务,请使用新接口json

百度老接口:https://dwz.cn/admin/create(短网址生成接口)
百度新接口:https://dwz.cn/admin/v2/create(短网址生成接口)复制代码

百度API:(未被收录的连接则须要收录后才能生成)

<?php
    $host = 'https://dwz.cn';
    $path = '/admin/v2/create';
    $url = $host . $path;
    $method = 'POST';
    $content_type = 'application/json';
    
    // TODO: 设置Token
    $token = '你的Token';
    
    // TODO:设置待注册长网址
    $bodys = array('url'=>'你的长网址'); 
    
    // 配置headers 
    $headers = array('Content-Type:'.$content_type, 'Token:'.$token);
    
    // 建立链接
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
    
    // 发送请求
    $response = curl_exec($curl);
    curl_close($curl);
    
    // 读取响应
    var_dump($response);
    ?>复制代码

响应结果示例

{
    "Code": 0,
    "ShortUrl": "https://dwz.cn/de3rp2Fl",
    "LongUrl": "http://www.baidu.com",
    "ErrMsg": ""
}复制代码

响应参数说明

字段 类型 说明
Code int 0:正常返回短网址
int -1:短网址生成失败
int -2:长网址不合法
int -3:长网址存在安全隐患
int -4:长网址插入数据库失败
int -5:长网址在黑名单中,不容许注册
ShortUrl string 短网址
LongUrl string 长网址(原网址)
ErrMsg string 错误信息

新浪API:

http://api.t.sina.com.cn/short_url/shorten.json (返回结果是JSON格式) 
http://api.t.sina.com.cn/short_url/shorten.xml (返回结果是XML格式)复制代码

请求参数: 小程序

source 申请应用时分配的AppKey,调用接口时表明应用的惟一身份。 微信小程序

url_long 须要转换的长连接,须要URLencoded,最多不超过20个。api

多个url参数须要使用以下方式请求:url_long=aaa&url_long=bbb

建立source方法

1.进入open.weibo.com/ ,选择菜单 微链接->网站接入。

2.点击当即接入,建立新应用,随便填写应用名称,点击建立。

3.建立成功后,AppKey就是source参数的值,能够用于请求建立短连接。

<?php

namespace App\Services;


class ShortUrlService
{
    /**
     * 调用新浪接口将长连接转为短连接
     * @param  string        $source    申请应用的AppKey
     * @param  array|string  $urlLong  长连接,支持多个转换(须要先执行urlencode)
     * @return array
     */
    public static function getSinaShortUrl($source, $urlLong)
    {
        // 参数检查
        if(empty($source) || !$urlLong){
            return false;
        }
        // 参数处理,字符串转为数组
        if(!is_array($urlLong)){
            $urlLong = array($urlLong);
        }
        // 拼接url_long参数请求格式
        $url_param = array_map(function($value){
            return '&url_long='.urlencode($value);
        }, $urlLong);
        $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 如下是公用API,暂时可用。如失效,注册新浪开发者账号便可

$source = config('app.sina');复制代码

单个连接转换

$urlLong = config('app.url');

$shortUrl = ShortUrlService::getSinaShortUrl($source, $urlLong);复制代码

返回结果

array:1 [
  0 => array:3 [
    "url_short" => "http://t.cn/*******"
    "url_long" => "http://***********.com/#/***********"
    "type" => 0
  ]
]复制代码

多个连接转换

$urlLong = [
            'http://www.***.com/article/1.html',
            'http://www.***.com/article/2.html',
            'http://www.***.com/index.html'
        ];

$shortUrl = ShortUrlService::getSinaShortUrl($source, $urlLong);复制代码

返回结果

array:3 [
  0 => array:3 [
    "url_short" => "http://t.cn/RD12"
    "url_long" => "http://www.***.com/article/1.html"
    "type" => 0
  ]
  1 => array:3 [
    "url_short" => "http://t.cn/RD134KV"
    "url_long" => "http://www.***.com/article/2.html"
    "type" => 0
  ]
  2 => array:3 [
    "url_short" => "http://t.cn/RA8u231F"
    "url_long" => "http://www.***.com/index.html"
    "type" => 0
  ]
]复制代码

经过上面的方法,能够很轻松的实现用php生成短连接网址的功能。

项目源码中含有短链接生成的api接口:github.com/WXiangQian/…

附加小程序二维码:

​​​​

相关文章
相关标签/搜索