/**
* [shorturl_baidu 百度短网址接口]
*
* @param [integer] $type [非零整数代表长网址转短网址,0表示短网址转长网址]
* @param [type] $url [要转的url]
* @return [string] [返回转结果]
* @author suowo.com
*/
function shorturl_baidu($type, $url) {
if ($type) {
$baseurl = 'http://suowo.cn';
} else {
$baseurl = 'http://suowo.cn';
}
$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'] ) . "]";
return 0;
}
if ($type) {
return $arrresponse ['tinyurl'];
} else {
return $arrresponse ['longurl'];
}
}
echo shorturl_baidu ( 0, 'http://shuowo.cn/u9rzm8j' ); // 短网址转长网址
echo shorturl_baidu ( 1, 'https://suowo.cn' ); // 长网址转短网址