<?php
$res = createShortUrl('https://www.apple.com/cn/macbook/');
if ($res['status'] == 'success') {
$shortUrl = $res['url'];
var_export($shortUrl);
} else {
var_export($res);
}
/**
* 生成短链接[百度]
* @param string $url 长网址
* @param string $alias 别名后缀,可自定义后缀,也可通过请求生成
* @return array array ( 'tinyurl' => '短网址', 'status' => 0, 'longurl' => '长网址', 'err_msg' => '', )
*/
function createShortUrl($url, $alias = '')
{
if (!$url) {
return array('status' => 'error');
}
$data = array(
'post_url' => 'http://dwz.cn/create.php',
'url' => $url,
'access_type' => 'web',
'alias' => $alias,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $data['post_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$res = curl_exec($ch);
$arr = json_decode($res, true);
if (isset($arr) && isset($arr['tinyurl']) && $arr['status'] == 0) {
return array(
'status' => 'success',
'url' => $arr['tinyurl'],
);
} else {
return array('status' => 'error');
}
}