Calling Baidu’s Short URL API to Generate Short URLs

11,270 1

Not long ago, I wrote an article titled “jQuery Online Generation of t.cn Sina Short URLs,” which called Sina Weibo’s short URL API. I gradually developed an interest in short URLs, so I specifically looked for some publicly available short URL APIs.

Today I’m going to talk about Baidu’s short URL API and share my experience and the completed code below. (Baidu short URLs support only a small number of domains. I’m not sure about the specific rules, so this article is not particularly useful and is for learning purposes only.)

Getting Started

First, the API request URL for Baidu short URLs is: http://dwz.cn/create.php

Only post submissions are allowed for the request method.

The request parameters include two items: url and alias. The first parameter is the long URL (required), and the second parameter is a custom URL (optional).

Returned data: data in JSON format.

status = 0 indicates an error. Check err_msg for the error message (UTF-8 encoding).

status = -1 indicates success. The returned tinyurl is the short URL.

Finished Code

PHP
<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$data=array('url'=>'http://www.baidu.com/');
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$strRes=curl_exec($ch);
curl_close($ch);
$arrResponse=json_decode($strRes,true);
if($arrResponse['status'] == 0)
{
    echo iconv('UTF-8','GBK',$arrResponse['err_msg']);
}
echo $arrResponse['tinyurl'].;  //输出短网址
?>

Baidu’s short URL service is not more useful than Sina’s, so I still recommend using Sina’s short URL service.

Comments

(1)
  1. Chrome 77 Windows 7

    一直用的雷虎短链

Leave a comment

© 2026 Carefree. All rights reserved.

Carefree is a personal site about technology and everyday life, documenting web development, backend, and DevOps practices, plus software tools, digital experiences, and thoughts beyond code.