教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP实现百度、网易、新浪短网址服务的API接口调用

PHP实现百度、网易、新浪短网址服务的API接口调用

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP实现百度、网易、新浪短网址服务的API接口调用等资源,欢迎您收藏本站,我们将为您提供最新的PHP实现百度、网易、新浪短网址服务的API接口调用资源
闲来蛋疼,看了几个短网址API服务,于是把它们整理出来,方便以后使用。目前,提供靠谱的短网址API接口的公司不多(谷歌、百度、新浪微博、网易等),而像腾讯微博、淘宝这几个巨头的短网址服务都是仅供内部使用。

1 谷歌、百度、网易、新浪短网址服务的API比较

百度短网址API接口完全对外开放,用户不需申请其开放平台的APPKEY,也不用采用OAuth的协议,因此相对简单方便;谷歌的短网址API接口有两种形式,一种类似于百度无需进行繁复的OAuth认证,不过限制比多;另一种是采用GAE平台OAuth2.0的认证方式,限制较少。新浪微博的短网址API接口服务也类似于谷歌,第一种只需要取得新浪微博开放平台的APPKEY即可使用,第二种是采用OAuth2.0认证的方式。网易只提供类似于新浪微博提供的第二种API接口调用方式,即需要申请APPKEY,不过申请非常容易通过,这点不同于新浪微博。值得一提的是,经博主测试,网易的短网址API接口貌似有bug。最终三个接口的测试效果如下图所示:

百度网易新浪微博短网址API接口
2 PHP实现百度短网址API接口调用

百度短网址的API接口封装不是很好,需要针对长网址转短网址和短网址转长网址请求不同的页面(create.php和query.php),另外官方的示例程序也有错误。

   

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy2324')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2324" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2"><?php
    /**
    * @author: vfhky 20130304 20:10
    * @description: PHP调用百度短网址API接口
    *     * @param string $type: 非零整数代表长网址转短网址,0表示短网址转长网址
    */
    function bdUrlAPI($type, $url){
    if($type)
    $baseurl = 'http://dwz.cn/create.php';
    else
    $baseurl = 'http://dwz.cn/query.php';
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$baseurl);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    if($type)
    $data=array('url'=>$url);
    else
    $data=array('tinyurl'=>$url);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $strRes=curl_exec($ch);
    curl_close($ch);
    $arrResponse=json_decode($strRes,true);
    if($arrResponse['status']!=0)
    {
    echo 'ErrorCode: ['.$arrResponse['status'].'] ErrorMsg: ['.iconv('UTF-8','GBK',$arrResponse['err_msg'])."]<br/>";
    return 0;
    }
    if($type)
    return $arrResponse['tinyurl'];
    else
    return $arrResponse['longurl'];
    }
    echo '<br/><br/>----------百度短网址API----------<br/><br/>';
    echo 'Long to Short: '.bdUrlAPI(1, 'http://www.jiaochengji.com').'<br/>';
    echo 'Short to Long: '.bdUrlAPI(0, 'http://dwz.cn/evlhW').'<br/><br/>';
    ?></td> </tr> </tbody> </table>

3 PHP实现网易短网址API接口调用

网易短网址API接口

用户首先需要申请一个appkey,申请地址是http://126.am/,登录进去即可申请,并且很快得到审核。不过,经过测试发现一个bug:用接口生成的短网址无法通过API接口还原为之前的长网址,提示“NOT_MATCH”(对应的官方说明是:Key和短地址不匹配,无法还原)。但是如上图所示,如果在http://126.am/user.action的页面生成的短网址却能够通过API还原为原来的长网址。

   

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy8206')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy8206" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2"><?php
    /**
    * @author: vfhky 20130304 20:10
    * @description: PHP调用网易短网址API接口
    *    * @param string $type: 非零整数代表长网址转短网址,0表示短网址转长网址
    */
    function wyUrlAPI($type,$url){
    if($type)
    $baseurl = 'http://126.am/api!shorten.action';
    else
    $baseurl = 'http://126.am/api!expand.action';
    /* 这是我申请的APPKEY,大家可以测试使用 */
    $key = '4f0c04771d4e40b4945afcfdc0337e3d';
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$baseurl);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    if($type)
    $data=array('longUrl'=>$url,'key'=>$key);
    else
    $data=array('shortUrl'=>$url,'key'=>$key);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $strRes=curl_exec($ch);
    curl_close($ch);
    $arrResponse=json_decode($strRes,true);
    if($arrResponse['status_code']!=200)
    {
    echo 'ErrorCode: ['.$arrResponse['status_code'].'] ErrorMsg: ['.iconv('UTF-8','GBK',$arrResponse['status_txt'])."]<br/>";
    return 0;
    }
    return $arrResponse['url'];
    }
    echo '<br/><br/>----------网易短网址API----------<br/><br/>';
    echo 'Long to Short: '.wyUrlAPI(1,'http://www.jiaochengji.com').'<br/>';
    echo 'Short to Long: '.wyUrlAPI(0,'http://126.am/huangky').'
    ';
    echo 'Short to Long: '.wyUrlAPI(0,'126.am/XRYsJ2').'<br/><br/>';
    ?></td> </tr> </tbody> </table>

4 PHP实现新浪微博短网址API接口调用

同样,用户首先需要申请一个新浪微博开放平台的appkey,申请地址是http://open.t.sina.com.cn/,不过审核相对严格而且比较慢。新浪微博短网址API接口有两种实现方式,第一种是原始的OAuth1.0的验证方式,比较简单,无需申请token,第二种是OAuth2.0的验证方式,这个需要access_token(虽然官方文档http://t.cn/8FgFoL8说可以像第一种那样直接通过appkey验证,但是测试不成功)。因此下面的示例采用的是第一种方式,即直接通过appkey验证。

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy6023')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy6023" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

 

    <?php
    /**
    * @author: vfhky 20130304 20:10
    * @description: PHP调用新浪短网址API接口
    *    * @param string $type: 非零整数代表长网址转短网址,0表示短网址转长网址
    */
    function xlUrlAPI($type,$url){
    /* 这是我申请的APPKEY,大家可以测试使用 */
    $key = '1562966081';
    if($type)
    $baseurl = 'http://api.t.sina.com.cn/short_url/shorten.json?source='.$key.'&url_long='.$url;
    else
    $baseurl = 'http://api.t.sina.com.cn/short_url/expand.json?source='.$key.'&url_short='.$url;
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL,$baseurl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $strRes=curl_exec($ch);
    curl_close($ch);
    $arrResponse=json_decode($strRes,true);
    if (isset($arrResponse->error) || !isset($arrResponse[0]['url_long']) || $arrResponse[0]['url_long'] == '')
    return 0;
    if($type)
    return $arrResponse[0]['url_short'];
    else
    return $arrResponse[0]['url_long'];
    }
    echo '<br/><br/>----------新浪短网址API----------<br/><br/>';
    echo 'Long to Short: '.xlUrlAPI(1,'http://www.jiaochengji.com').'<br/>';
    echo 'Short to Long: '.xlUrlAPI(0,'http://t.cn/8FdW1rm').'<br/><br/>';
    ?>

</td> </tr> </tbody> </table>

5 后记

综上,百度的短网址API相对方便,而且限制较少;新浪和网易的API接口相对麻烦;网易的短网址API是唯一具备API请求统计功能的,但很容易受到“请求过于频繁而遭到REQUEST_LIMIT”。另外,对于任何API接口的调试工作,一定要使用其接口提供的错误信息,例如上面百度接口的$arrResponse['status']字段、网易的$arrResponse['status_code']字段。

原文转自:http://www.huangkeye.cn/web/php/839.html

 

您可能感兴趣的文章:
PHP实现百度、网易、新浪短网址服务的API接口调用
PHP如何实现短网址
go比php好在哪里
JQuery与Ajax调用新浪API获取短网址的代码
怎么让百度快速收录新网站
golang微服务框架对比_最强开源微服务框架,全网独家整理
php中实现api接口思路介绍
支持多语言的微服务框架Tars-Go
基于Golang设计一套微服务架构[转]
PHP 微服务集群搭建 - Hyperf

[关闭]
~ ~