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

Generate short URL using DWZ.CN

大家讲道理
Release: 2016-11-08 11:50:14
Original
4951 people have browsed it

<?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

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template