<?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'); } }