<?php

/**
 * 短链类
 * Class ShortenLinkModel
 * Created by: Owen
 * Created on: 2020/8/3 10:16
 */
class ShortenLinkModel {
    private $api       = "http://api.bit.ly/v3/";
    private $oauthApiUrl = "https://api-ssl.bit.ly/v3/";
    private $oauthAccessTokenUrl = "https://api-ssl.bit.ly/oauth/";
    private $username        = "zaqx000@gmail.com";
    private $password        = "superantNo1";
    private $access_token    = "2795c70dc581c88b22290efc6972611c2c98ddcb";

    public function shortUrl($longUrl,$domian = 'bit.ly'){
        $params = array();
        $params['access_token'] = $this->access_token;
        $params['longUrl'] = $longUrl;
        $params['domain']  = $domian;
        $results = self::get('shorten', $params);
        if($results['status_code'] == 200){
            return $results['data'];
        }
        return false;
    }

    public function oauthAccessTokenUrl($code, $redirect, $client_id, $client_secret) {
        $results = array();
        $url     = $this->oauthAccessTokenUrl . "access_token";
        $params = array();
        $params['client_id'] = $client_id;
        $params['client_secret'] = $client_secret;
        $params['code'] = $code;
        $params['redirect_uri'] = $redirect;
        $output = self::post_curl($url, $params);
        $parts = explode('&', $output);
        foreach ($parts as $part) {
            $bits = explode('=', $part);
            $results[$bits[0]] = $bits[1];
        }
        return $results;
    }

    public function oauthAccessTokenUrl_via_password($username, $password, $client_id, $client_secret) {
        $url = $this->oauthAccessTokenUrl . "access_token";
        $headers = array();
        $headers[] = 'Authorization: Basic '.base64_encode($client_id . ":" . $client_secret);
        $params = array();
        $params['grant_type'] = "password";
        $params['username'] = $this->username;
        $params['password'] = $this->password;
        $output = self::post_curl($url, $params, $headers);
        $decoded_output = json_decode($output,1);
        $results = array(
            "access_token" => $decoded_output['access_token']
        );
        return $results;
    }

    public function get($endpoint, $params, $complex=false) {
        if ($complex) {
            $url_params = "";
            foreach ($params as $key => $val) {
                if (is_array($val)) {
                    $recs = array();
                    foreach ($val as $rec) {
                        $tmp = explode('/', $rec);
                        $tmp = array_reverse($tmp);
                        array_push($recs, $tmp[0]);
                    }
                    $val = implode('&' . $key . '=', $recs);
                }
                $url_params .= '&' . $key . "=" . $val;
            }
            $url = $this->oauthApiUrl . $endpoint . "?" . substr($url_params, 1);
        } else {
            $url = $this->oauthApiUrl . $endpoint . "?" . http_build_query($params);
        }
        $result = json_decode(self::get_curl($url), true);
        return $result;
    }


    public function get_curl($uri) {
        $output = "";
        try {
            $ch = curl_init($uri);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 4);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            $output = curl_exec($ch);
        } catch (Exception $e) {
        }
        return $output;
    }
    
    public function post_curl($uri, $fields, $header_array = array()) {
        $output = "";
        $fields_string = "";
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
        rtrim($fields_string,'&');
        try {
            $ch = curl_init($uri);
            if(is_array($header_array) && !empty($header_array)){
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
            }
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch,CURLOPT_POST,count($fields));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            $output = curl_exec($ch);
        } catch (Exception $e) {
        }
        return $output;
    }
}

?>