PHP代码实现短链接生成 - 使用API接口
'https://www.baidu.com', // 需要缩短的链接
'type' => 'json' // 返回格式为JSON
);
// 发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params)); // 拼接请求参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
// 解析返回结果
$result = json_decode($output, true);
if($result['code'] == 200){
// 生成成功,输出短链接
echo '
{$result['msg']},短链接为:{$result['url']}
';
}else{
// 生成失败,输出错误信息
echo '{$result['msg']}
';
}
?>
原文地址: http://www.cveoy.top/t/topic/l45x 著作权归作者所有。请勿转载和采集!