Home php教程 PHP源码 Generate short URL using DWZ.CN

Generate short URL using DWZ.CN

Nov 08, 2016 am 11:50 AM

<?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, true);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $param);
        $strRes = curl_exec($ch);
        curl_close( $ch );
        $arrResponse = json_decode( $strRes, true );
        // if( $arrResponse[&#39;status&#39;]==0 ) {
        //  echo iconv(&#39;UTF-8&#39;,&#39;GBK&#39;, $arrResponse[&#39;err_msg&#39;])."\n";
        // } else {
        //  return $arrResponse;
        // }
        return $arrResponse;
    }
    // --------------------------------------------------------------------
    /**
     * 使用DWZ生产短网址服务
     *
     * @see    http://dwz.cn/
     * @param  string $url
     * @return array|bool
     */
    public static function createTinyUrl( $url=&#39;&#39; ){
        if( $url ){
            $targetURL = &#39;http://dwz.cn/create.php&#39;;
            $param = array(
                &#39;url&#39; => $url,
            );
            $result = self::httpPost( $targetURL, $param );
            if( $result[&#39;status&#39;] == 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] => 
// )
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)