PHP中的短网址怎么利用DWZ.CN服务进行生成

PHP中的短网址怎么利用DWZ.CN服务进行生成

这期内容当中小编将会给大家带来有关PHP中的短网址怎么利用DWZ.CN服务进行生成,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

使用DWZ.CN生成短网址

<&#63;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;
  }
  // --------------------------------------------------------------------
  /**
   * 使用DWZ生产短网址服务
   *
   * @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;
      }
    }
  }
  // --------------------------------------------------------------------
}

测试

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

上述就是小编为大家分享的PHP中的短网址怎么利用DWZ.CN服务进行生成了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。