PHP uses DWZ. CN service to generate short URLs

  • 2021-12-19 06:00:28
  • OfStack

Using DWZ. CN to generate short URLs


<?php
/**
 * FunctionHelper
 */
class FunctionHelper {
  // --------------------------------------------------------------------
  /**
   * httpPost
   *
   * @param string $url
   * @param array $param
   * @return array|bool
   */
  public static function httpPost( $url,array $param ){
  	if( empty($url) || empty($param) ){
  		return false;
  	}
    $ch = curl_init();
		curl_setopt( $ch,CURLOPT_URL,$url);
		curl_setopt( $ch,CURLOPT_POST,true);
		curl_setopt( $ch,CURLOPT_RETURNTRANSFER,CURLOPT_POSTFIELDS,$param);
		$strRes = curl_exec($ch);
		curl_close( $ch );
		$arrResponse = json_decode( $strRes,true );
		// if( $arrResponse['status']==0 ) {
		// 	echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";
		// } else {
		// 	return $arrResponse;
		// }
		return $arrResponse;
  }
  // --------------------------------------------------------------------
  /**
   *  Use DWZ Production of short URL service 
   *
   * @see  http://dwz.cn/
   * @param string $url
   * @return array|bool
   */
  public static function createTinyUrl( $url='' ){
    if( $url ){
      $targetURL = 'https://dwz.cn/admin/v2/create';
      $param = array(
        'url' => $url,);
      $result = self::httpPost( $targetURL,$param );
      if( $result['status'] == 0 ){
        return $result;
      } else {
        return false;
      }
    }
  }
  // --------------------------------------------------------------------
}

Test


$strLongUrl = "https://www.ofstack.com";
$arrTinyUrlResult = FunctionHelper::createTinyUrl( $strLongUrl );
print_r($arrTinyUrlResult);
// $ php dwz_test.php 
// Array
// (
//   [tinyurl] => https://dwz.cn/JGCv8rpm
//   [status] => 0
//   [longurl] => https://www.ofstack.com
//   [err_msg] => 
// )
 

Summarize

The above is the site for you to collect collated PHP using DWZ. CN service to generate all the short URL content, I hope the article can help you solve the use of DWZ. CN to generate short URL encountered program development problems.


Related articles: