長連接轉換成短連接

使用的是百度短網址
百度短網址文檔: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);
    ?>

返回結果
{"Code":0,"ShortUrl":"https://dwz.cn/EiKqMc4v","LongUrl":"http://www.yixianglife.com","ErrMsg":""}'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章