- Interface API Document Address: http://dwz.cn/#/apidoc?_k=i9ev5p
- Code Demo
Header("content-type:text/html; Charset=utf-8 "); /** * Generate short URLs*/ //short URL Generation interface address & passed Parameters $url= "Http://dwz.cn/admin/create"; $param= [ "url" = "http://www.baidu.com", ]; //Curl Initialize &curl configuration $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); //perform a Curl session $ret= Curl_exec ($ch); //get information about a Curl connection resource handle $retInfo= Curl_getinfo ($ch); if($retInfo[' http_code '] = = 200){ $data= Json_decode ($ret,true); /** * code:0: normal return short URL,-1: Short URL generation failed,-2: Long URL is illegal,-3: Long URLs have security implications,-4: Long URLs inserted into the database failed, 5: Long URLs in the blacklist, not allowed to register; * Shorturl: Short URL * longurl: Long URL (original URL) * ErrMsg: Error message*/ if($data[' Code ']! = 0){ Echo' Short URL generation failed with the error reason: '.$data[' ErrMsg ']; }Else{ Echo' Short URL generated successfully! ' by ['.$data[' Longurl ']. The resulting short URL is: ['.$data[' Shorturl ']. '] '; } }Else{ Echo' Make short URL failed '; }Echo"; /** * Short URL restore*/ //Short URL Restore interface address & passed Parameters $url= "Http://dwz.cn/admin/query"; $param= [ "Shorturl" = "Http://dwz.cn/de3rp2Fl", ]; //Curl Initialize &curl configuration $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); //perform a Curl session $ret= Curl_exec ($ch); //get information about a Curl connection resource handle $retInfo= Curl_getinfo ($ch); if($retInfo[' http_code '] = = 200){ $data= Json_decode ($ret,true); /** * code:0: normal return short URL,-1: Short URL corresponding to the long URL is not legal,-2: Short URL does not exist,-3: Query the short URL is not legal; * Shorturl: Short URL * longurl: Long URL (original URL) * ErrMsg: Error message*/ if($data[' Code ']! = 0){ Echo' Short URL recovery failed with the error reason: '.$data[' ErrMsg ']; }Else{ Echo' Short URL restored successfully! ' by ['.$data[' Shorturl ']. The restored short URL is: ['.$data[' Longurl ']. '] '; } }Else{ Echo' Make short URL failed '; }
You can also use the Ajax calling interface to get short links
PHP calls Baidu API to generate short URLs & restore long URLs based on short URLs