Convert long connection to short connection

Baidu Short URL is used.
Baidu Short URL Document: https://dwz.cn/console/apidoc

<?php
    $host = 'https://dwz.cn';
    $path = '/admin/v2/create';
    $url = $host . $path;
    $method = 'POST';
    $content_type = 'application/json';
    
    // TODO: 设置Token
    $token = 'd60df1791aa7b4201d74cb48e1c2dd1b';
    
    // TODO:设置待注册长网址
    $bodys = array('Url'=>'http://www.yixianglife.com', 'TermOfValidity'=>'long-term');
    
    // 配置headers 
    $headers = array('Content-Type:'.$content_type, 'Token:'.$token);
    
    // 创建连接
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
    
    // 发送请求
    $response = curl_exec($curl);
    curl_close($curl);
    
    // 读取响应
    var_dump($response);
    ?>

Return result
{"Code":0,"ShortUrl":"https://dwz.cn/EiKqMc4v","LongUrl":"http://www.yixianglife.com","ErrMsg":""}'

Guess you like

Origin blog.csdn.net/kevlin_V/article/details/105568232