/**
* [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'
); // 长网址转短网址