使用dwz.cn生成短网址

<?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] => 
// )
 

总结

以上是www.887551.com为你收集整理的php利用dwz.cn服务生成短网址全部内容,希望文章能够帮你解决使用dwz.cn生成短网址所遇到的程序开发问题。