调用新浪接口生成短链接

  • 黄楚恒
  • 2017-11-10 10:00:08
  • PHP
  • php
http://blog.csdn.net/fdipzone/article/details/70832761
/**
 * [getSinaShortUrl 调用新浪接口生成短链接]
 * @param  [type] $source   [AppKey]
 * @param  [type] $url_long [长链接]
 * @return [type]           [数组]
 */
function getSinaShortUrl($source, $url_long){

    // 参数检查
    if(empty($source) || !$url_long){
        return false;
    }

    // 拼接url_long参数请求格式
    $url_param = '&url_long='.urlencode($url_long);

    // 新浪生成短链接接口
    $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;

}

Copyright © 2016-2019 709173702@qq.com 版权所有  ICP证粤ICP备16117826号-1
联系邮箱:709173702@qq.com