溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何用代碼實(shí)現(xiàn)聚合接口對接

發(fā)布時間:2021-10-14 09:41:40 來源:億速云 閱讀:97 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“如何用代碼實(shí)現(xiàn)聚合接口對接”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

<?php
/**
 * 聚合接口模型.
 *
 * @version TS4.5
 * @name JuheModel
 *
 * @author Foreach
 */
class Juhe
{
    public $appkey_config = array(
        'tel'=>'appkey',
        'flow'=>'appkey',
        'sinopec'=>'appkey',
    );
 
    public $openid = 'JH160141dd01f07b3149d99dad44802431';
    public $appkey = '';
 
    /**
     * juheModel constructor.
     * @param string $type tel 話費(fèi) flow 流量 sinopec 油卡
     */
    public function __construct($type)
    {
        switch($type){
            case 'tel':
                $this ->appkey = $this ->appkey_config['tel'];
                break;
            case 'flow':
                $this ->appkey = $this ->appkey_config['flow'];
                break;
            case 'sinopec':
                $this ->appkey = $this ->appkey_config['sinopec'];
                break;
        }
    }
 
    /**
     * 【油卡】加油卡充值
     * @param $proid
     * @param $cardnum
     * @param $orderid
     * @param $game_userid
     * @param $gasCardTel
     * @param $gasCardName
     * @param $chargeType
     * @return mixed
     */
    public function order($proid,$cardnum,$orderid,$game_userid,$gasCardTel,$gasCardName,$chargeType)
    {
        $url = "http://op.juhe.cn/ofpay/sinopec/onlineorder";
        $newkey = $this->openid.$this->appkey.$proid.$cardnum.$game_userid.$orderid;
        $sign= md5($newkey);
        $params = array(
            "proid" => $proid,//產(chǎn)品id:10000(中石化50元加油卡)、10001(中石化100元加油卡)、10003(中石化500元加油卡)、10004(中石化1000元加油卡)、10007(中石化任意金額充值)、10008(中石油任意金額充值)
            "cardnum" => $cardnum,//充值數(shù)量 任意充 (整數(shù)(元)),其余面值固定值為1
            "orderid" => $orderid,//商家訂單號,8-32位字母數(shù)字組合
            "game_userid" => $game_userid,//加油卡卡號,中石化:以100011開頭的卡號、中石油:以9開頭的卡號
            "gasCardTel" => $gasCardTel,//持卡人手機(jī)號碼
            "gasCardName" => $gasCardName,//持卡人姓名
            "chargeType" => $chargeType,//加油卡類型 (1:中石化、2:中石油;默認(rèn)為1)
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
            "sign" => $sign,//校驗(yàn)值,md5(OpenID+key+proid+cardnum+game_userid+orderid),OpenID在個人中心查詢
        );
        $content = $this->juhecurl($url, $params);
        $result = json_decode($content, true);
        return $result;
    }
 
    /**
     * 【油卡】油卡查詢訂單狀態(tài)
     * @param $orderid
     * @return mixed
     */
    public function ordersta($orderid)
    {
        $url = "http://op.juhe.cn/ofpay/sinopec/ordersta";
        $params = array(
            "orderid" => $orderid,//商家訂單號,8-32位字母數(shù)字組合
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $content = $this->juhecurl($url, $params);
        $result = json_decode($content, true);
        return $result;
    }
 
    /**
     * 【流量】全部流量套餐列表
     * @return mixed
     */
    public function flowlist()
    {
        //************1.全部流量套餐列表************
        $url = "http://v.juhe.cn/flow/list";
        $params = array(
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
    }
 
    /**
     * 【流量】檢測號碼支付的流量套餐
     * @param $phone
     * @return mixed
     */
    public function telcheck($phone)
    {
        //************2.檢測號碼支持的流量套餐************
        $url = "http://v.juhe.cn/flow/telcheck";
        $params = array(
            "phone" => $phone,//要查詢的手機(jī)號碼
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【流量】檢測號碼購買的流量是否存在
     * @param $phone
     * @param $price
     * @return bool
     */
    public function telpricecheck($phone,$price)
    {
        //************2.檢測號碼支持的流量套餐************
        $url = "http://v.juhe.cn/flow/telcheck";
        $params = array(
            "phone" => $phone,//要查詢的手機(jī)號碼
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        if($result){
            if($result['error_code']=='0'){
                $product=$result['result'][0];
                $flows=$result['result'][0]['flows'];
                if($flows && count($flows)>0)
                {
                    foreach ($flows as $flow)
                    {
                        if($flow['p']==$price)
                        {
                            $product['item']=$flow;
                            return $product;
                        }
                    }
                }
                return false;
            }else{
                return false;
            }
        }else{
            return false;
        }
        //**************************************************
    }
 
    /**
     * 【流量】提交流量充值
     * @param $phone
     * @param $pid
     * @param $orderid
     * @return mixed
     */
    public function recharge($phone,$pid,$orderid)
    {
        //************3.提交流量充值************
        $newkey = $this->openid.$this->appkey.$phone.$pid.$orderid;
        $sign= md5($newkey);
        $url = "http://v.juhe.cn/flow/recharge";
        $params = array(
            "phone" => $phone,//需要充值流量的手機(jī)號碼
            "pid" => $pid,//流量套餐ID
            "orderid" => $orderid,//自定義訂單號,8-32字母數(shù)字組合
            "key" => $this->appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
            "sign" => $sign,//校驗(yàn)值,md5(<b>OpenID</b>+key+phone+pid+orderid),結(jié)果轉(zhuǎn)為小寫
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
    /**
     * 【流量】訂單狀態(tài)查詢
     * @param $orderid
     * @return mixed
     */
    public function orderstaflow($orderid)
    {
        //************4.訂單狀態(tài)查詢************
        $url = "http://v.juhe.cn/flow/ordersta";
        $params = array(
            "orderid" => $orderid,//商家訂單號,8-32位字母數(shù)字組合,由您自己生成
            "key"     => $this ->appkey, //應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【話費(fèi)】檢測手機(jī)號是否能充值
     * @param $phone
     * @param $cardnum
     * @return mixed
     */
    public function telcheckmobile($phone,$cardnum)
    {
        //************1.檢測手機(jī)號碼是否能充值************
        $url = "http://op.juhe.cn/ofpay/mobile/telcheck";
        $params = array(
            "phoneno" => $phone,//要查詢的手機(jī)號碼
            "cardnum" => $cardnum,//充值金額,目前可選:10、20、30、50、100、300
            "key"     => $this ->appkey //應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【話費(fèi)】根據(jù)手機(jī)號和面值查詢商品
     * @param $phone
     * @param $cardnum
     * @return mixed
     */
    public function telquery($phone,$cardnum)
    {
        //************2.根據(jù)手機(jī)號和面值查詢商品************
        $url = "http://op.juhe.cn/ofpay/mobile/telquery";
        $params = array(
            "phoneno" => $phone,//要查詢的手機(jī)號碼
            "cardnum" => $cardnum,//充值金額,目前可選:10、20、30、50、100、300
            "key"     => $this ->appkey //應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【話費(fèi)】手機(jī)直充接口
     * @param $phone
     * @param $cardnum
     * @param $orderid
     * @return mixed
     */
    public function onlineorder($phone,$cardnum,$orderid)
    {
        //************3.手機(jī)直充接口************
        $url = "http://op.juhe.cn/ofpay/mobile/onlineorder";
        $newkey = $this->openid.$this->appkey.$phone.$cardnum.$orderid;
        $sign= md5($newkey);
        $params = array(
            "phoneno" => $phone,//要查詢的手機(jī)號碼
            "cardnum" => $cardnum,//充值金額,目前可選:10、20、30、50、100、300
            "orderid" => $orderid,//商家訂單號,8-32位字母數(shù)字組合,由您自己生成
            "key"     => $this ->appkey, //應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
            "sign"     => $sign //校驗(yàn)值,md5(OpenID+key+phoneno+cardnum+orderid),OpenID在個人中心查詢
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【話費(fèi)】訂單狀態(tài)查詢
     * @param $orderid
     * @return mixed
     */
    public function orderstamobile($orderid)
    {
        //************4.訂單狀態(tài)查詢************
        $url = "http://op.juhe.cn/ofpay/mobile/ordersta";
        $params = array(
            "orderid" => $orderid,//商家訂單號,8-32位字母數(shù)字組合,由您自己生成
            "key"     => $this ->appkey, //應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        return $result;
        //**************************************************
    }
 
    /**
     * 【公共】請求接口返回內(nèi)容
     * @param  string $url [請求的URL地址]
     * @param  string $params [請求的參數(shù)]
     * @param  int $ispost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url,$params='',$ispost=0){
        $params = htmlspecialchars_decode($params);
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 3600 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 3600);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , true);
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            $paramstring = http_build_query($params);
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $paramstring );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        logInfo($url.'-請求地址','juhe_push','JuheLog');
        logInfo(json_encode($params).'-請求數(shù)據(jù)','juhe_push','JuheLog');
        logInfo($response.'-返回數(shù)據(jù)','juhe_push','JuheLog');
        if ($response === FALSE) {
            return false;
        }
        //$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        //$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
 
}

“如何用代碼實(shí)現(xiàn)聚合接口對接”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI