使用DWZ.CN生成短网址

前端之家收集整理的这篇文章主要介绍了使用DWZ.CN生成短网址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

使用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 = 'http://dwz.cn/create.PHP';
            $param = array(
                'url' => $url,);
            $result = self::httpPost( $targetURL,$param );
            if( $result['status'] == 0 ){
                return $result;
            } else {
                return false;
            }
        }
    }
    // --------------------------------------------------------------------
}

// 测试
$strLongUrl = "http://my.oschina.net/wangyongtao";
$arrTinyUrlResult = FunctionHelper::createTinyUrl( $strLongUrl );
print_r($arrTinyUrlResult);
// $ PHP dwz_test.PHP 
// Array
// (
//     [tinyurl] => http://dwz.cn/30R23W
//     [status] => 0
//     [longurl] => http://my.oschina.net/wangyongtao
//     [err_msg] => 
// )

<?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,$url);
		curl_setopt( $ch,true);
		curl_setopt( $ch,$param);
		$strRes = curl_exec($ch);
		curl_close( $ch );
		$arrResponse = json_decode( $strRes,true );
		// if( $arrResponse['status']==0 ) {
		// 	echo iconv('UTF-8',$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 = 'http://dwz.cn/create.PHP';
            $param = array(
                'url' => $url,);
            $result = self::httpPost( $targetURL,$param );
            if( $result['status'] == 0 ){
                return $result;
            } else {
                return false;
            }
        }
    }
	// --------------------------------------------------------------------
}

// 测试
$strLongUrl = "http://my.oschina.net/wangyongtao";
$arrTinyUrlResult = FunctionHelper::createTinyUrl( $strLongUrl );
print_r($arrTinyUrlResult);
// $ PHP dwz_test.PHP 
// Array
// (
//     [tinyurl] => http://dwz.cn/30R23W
//     [status] => 0
//     [longurl] => http://my.oschina.net/wangyongtao
//     [err_msg] => 
// )



以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的PHP相关文章