Maison > php教程 > php手册 > le corps du texte

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

WBOY
Libérer: 2016-05-25 16:45:21
original
2341 Les gens l'ont consulté

看了几个短网址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 = &#39;http://dwz.cn/create.php&#39;;
    else $baseurl = &#39;http://dwz.cn/query.php&#39;;
    $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(
        &#39;url&#39; => $url
    );
    else $data = array(
        &#39;tinyurl&#39; => $url
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $strRes = curl_exec($ch);
    curl_close($ch);
    $arrResponse = json_decode($strRes, true);
    if ($arrResponse[&#39;status&#39;] != 0) {
        echo &#39;ErrorCode: [&#39; . $arrResponse[&#39;status&#39;] . &#39;] ErrorMsg: [&#39; . iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $arrResponse[&#39;err_msg&#39;]) . "]<br/>";
        return 0;
    }
    if ($type) return $arrResponse[&#39;tinyurl&#39;];
    else return $arrResponse[&#39;longurl&#39;];
}
echo &#39;<br/><br/>----------百度短网址API----------<br/><br/>&#39;;
echo &#39;Long to Short: &#39; . bdUrlAPI(1, &#39;http://www.111cn.net&#39;) . &#39;<br/>&#39;;
echo &#39;Short to Long: &#39; . bdUrlAPI(0, &#39;http://dwz.cn/evlhW&#39;) . &#39;<br/><br/>&#39;;
?>
Copier après la connexion

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

网易短网址API接口

用户首先需要申请一个appkey,申请地址是http://126.am/,登录进去即可申请,并且很快得到审核,不过,经过测试发现一个bug,用接口生成的短网址无法通过API接口还原为之前的长网址,提示“NOT_MATCH”(对应的官方说明是,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 = &#39;http://126.am/api!shorten.action&#39;;
    else $baseurl = &#39;http://126.am/api!expand.action&#39;;
    /* 这是我申请的APPKEY,大家可以测试使用 */
    $key = &#39;4f0c04771d4e40b4945afcfdc0337e3d&#39;;
    $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(
        &#39;longUrl&#39; => $url,
        &#39;key&#39; => $key
    );
    else $data = array(
        &#39;shortUrl&#39; => $url,
        &#39;key&#39; => $key
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $strRes = curl_exec($ch);
    curl_close($ch);
    $arrResponse = json_decode($strRes, true);
    if ($arrResponse[&#39;status_code&#39;] != 200) {
        echo &#39;ErrorCode: [&#39; . $arrResponse[&#39;status_code&#39;] . &#39;] ErrorMsg: [&#39; . iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $arrResponse[&#39;status_txt&#39;]) . "]<br/>";
        return 0;
    }
    return $arrResponse[&#39;url&#39;];
}
echo &#39;<br/><br/>----------网易短网址API----------<br/><br/>&#39;;
echo &#39;Long to Short: &#39; . wyUrlAPI(1, &#39;http://www.111cn.net&#39;) . &#39;<br/>&#39;;
echo &#39;Short to Long: &#39; . wyUrlAPI(0, &#39;http://126.am/huangky&#39;) . &#39; 
&#39;;
echo &#39;Short to Long: &#39; . wyUrlAPI(0, &#39;126.am/XRYsJ2&#39;) . &#39;<br/><br/>&#39;;
?>
Copier après la connexion

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 = &#39;1562966081&#39;;
    if ($type) $baseurl = &#39;http://api.t.sina.com.cn/short_url/shorten.json?source=&#39; . $key . &#39;&url_long=&#39; . $url;
    else $baseurl = &#39;http://api.t.sina.com.cn/short_url/expand.json?source=&#39; . $key . &#39;&url_short=&#39; . $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][&#39;url_long&#39;]) || $arrResponse[0][&#39;url_long&#39;] == &#39;&#39;) return 0;
    if ($type) return $arrResponse[0][&#39;url_short&#39;];
    else return $arrResponse[0][&#39;url_long&#39;];
}
echo &#39;<br/><br/>----------新浪短网址API----------<br/><br/>&#39;;
echo &#39;Long to Short: &#39; . xlUrlAPI(1, &#39;http://www.111cn.net&#39;) . &#39;<br/>&#39;;
echo &#39;Short to Long: &#39; . xlUrlAPI(0, &#39;http://t.cn/8FdW1rm&#39;) . &#39;<br/><br/>&#39;;
?>
Copier après la connexion

5 后记

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


文章网址:

随意转载^^但请附上教程地址。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!