標籤:連結 產生 param 錯誤 bubuko pid 串連 執行 short
- 介面api文檔地址:http://dwz.cn/#/apidoc?_k=i9ev5p
- 代碼demo
header("Content-type: text/html; charset=utf-8"); /** * 產生短網址 */ //短網址產生介面地址&傳遞的參數 $url = "http://dwz.cn/admin/create"; $param = [ "url"=>"http://www.baidu.com", ]; //curl初始化&curl配置 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //執行cURL會話 $ret = curl_exec($ch); //擷取cURL串連資源控制代碼的資訊 $retInfo = curl_getinfo($ch); if($retInfo[‘http_code‘] == 200){ $data = json_decode($ret, true); /** * Code:0:正常返回短網址,-1:短網址產生失敗,-2:長網址不合法,-3:長網址存在安全隱患,-4:長網址插入資料庫失敗,-5:長網址在黑名單中,不允許註冊; * ShortUrl:短網址 * LongUrl:長網址(原網址) * ErrMsg:錯誤資訊 */ if($data[‘Code‘] != 0){ echo ‘短網址產生失敗,錯誤原因為:‘ . $data[‘ErrMsg‘]; }else{ echo ‘短網址產生成功!由[‘.$data[‘LongUrl‘].‘]產生的短網址為:[‘.$data[‘ShortUrl‘].‘]‘; } }else{ echo ‘make short url failed‘; }echo "<hr>"; /** * 短網址還原 */ //短網址還原介面地址&傳遞的參數 $url = "http://dwz.cn/admin/query"; $param = [ "shortUrl"=>"http://dwz.cn/de3rp2Fl", ]; //curl初始化&curl配置 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //執行cURL會話 $ret = curl_exec($ch); //擷取cURL串連資源控制代碼的資訊 $retInfo = curl_getinfo($ch); if($retInfo[‘http_code‘] == 200){ $data = json_decode($ret, true); /** * Code:0:正常返回短網址,-1:短網址對應的長網址不合法,-2:短網址不存在,-3:查詢的短網址不合法; * ShortUrl:短網址 * LongUrl:長網址(原網址) * ErrMsg:錯誤資訊 */ if($data[‘Code‘] != 0){ echo ‘短網址恢複失敗,錯誤原因為:‘ . $data[‘ErrMsg‘]; }else{ echo ‘短網址恢複成功!由[‘.$data[‘ShortUrl‘].‘]恢複的短網址為:[‘.$data[‘LongUrl‘].‘]‘; } }else{ echo ‘make short url failed‘; }
還可以使用ajax調用介面擷取短連結
PHP調用百度api產生短網址&根據短網址恢複長網址