SoFunction
Updated on 2025-04-04

PHP uses services to generate short URLs

Generate short URLs using

<?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 to produce short URL services
    *
    * @see /
    * @param string $url
    * @return array|bool
    */
  public static function createTinyUrl( $url='' ){
    if( $url ){
      $targetURL = '/admin/v2/create';
      $param = array(
        'url' => $url,);
      $result = self::httpPost( $targetURL,$param );
      if( $result['status'] == 0 ){
        return $result;
      } else {
        return false;
      }
    }
  }
  // --------------------------------------------------------------------
}

test

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

Summarize

The above is all the content of PHP using services to generate short URLs that I have collected and compiled for you. I hope the article can help you solve the program development problems encountered when using short URLs to generate short URLs.