首页 > php代码 > 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),另外官方的示例程序也有错误。

<?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.phprm.com') . '<br/>';
echo 'Short to Long: ' . bdUrlAPI(0, 'http://dwz.cn/29uQh7') . '<br/><br/>';
?>

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

网易短网址API接口

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

<?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.phprm.com') . '<br/>';
echo 'Short to Long: ' . wyUrlAPI(0, 'http://126.am/www.phprm.com') . '
';
echo 'Short to Long: ' . wyUrlAPI(0, '126.am/www.phprm.com') . '<br/><br/>';
?>

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

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

<?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.phprm.com') . '<br/>';
echo 'Short to Long: ' . xlUrlAPI(0, 'http://t.cn/R7bPXDC') . '<br/><br/>';
?>

5 后记

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

文章链接:http://www.phprm.com/code/58446.html

随便收藏,请保留本文地址!

标签:iconv curl_setopt

相关文章

发表留言