【laravel】クラウドメール検証の統合---サードパーティクラスの導入方法

7785 ワード

一.クラスをカスタマイズしました(AppHelpersでクラスCloudPiece.phpをカスタマイズするなど)
systemKey = "996d6c5c42cec80d5499c9fcbd756183"; //     apikey(https://www.yunpian.com)       
        $this->operationKey = "1a1143af14792926772bc492154eeb43";//
//        $mobile = "18010798215"; //          
        //$text="        , /        ,  **APP        http://t.cn/ESicmsb  T  ";
//        $text = "     , /  **         ,  APP   /        http://t.cn/ESicmsb  T  ";
        $ch = curl_init();

        /*        */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        /*          */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /*       */
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /*        */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $this->ch = $ch;
    }

    /**
     *       
     * @param $text
     * @param $mobile
     * @return bool
     */
    public function sendSystemSMS($text, $mobile)
    {
        $data = array('text' => $text, 'apikey' => $this->systemKey, 'mobile' => $mobile);
        $json_data = $this->send($this->ch, $data);//"{"http_status_code":400,"code":5,"msg":"        ","detail":"           "}"
        //    
        Log::info($json_data);
        $this->storeLog($json_data,'system',$data);
        if (json_decode($json_data)->code == 0) {
            return true;
        }
        return false;
    }

    /**
     *       
     * @param $text
     * @param $mobile
     */
    public function sendOperationSMS($text, $mobile)
    {
        $data = array('text' => $text, 'apikey' => $this->operationKey, 'mobile' => $mobile);
        $json_data = $this->send($this->ch, $data);//"{"http_status_code":400,"code":5,"msg":"        ","detail":"           "}"
        //    
        Log::info($data);
        Log::info($json_data);
        $this->storeLog($json_data,'operation',$data);
    }

    /************************************************************************************/
//    
    function get_user($ch, $apikey)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function tpl_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL,
            'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function voice_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function notify_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result, $error);
        return $result;
    }

    function checkErr($result, $error)
    {
        if ($result === false) {
            echo 'Curl error: ' . $error;
        } else {
            //echo '          ';
        }
    }

    /**
     *     
     * @param $data
     * @param string $name
     * @param $request
     */
    public function storeLog($data,$name = 'system',$request = '')
    {
        $path = 'logs/sms/'.$name.'.log';
        $log = new Logger($name);
        $log->pushHandler(
            new StreamHandler(
                storage_path($path),
                100
            )
        );
        if($request){
            $log->addInfo(json_encode($request,JSON_UNESCAPED_UNICODE));
        }
        $log->addDebug($data);
    }

}

二.コンテナに入れた後-LaravelのFacade呼び出しを使用
Facades(読み方:/fəˈsód/)は、アプリケーションのサービスコンテナで使用可能なクラスに「静的」インタフェースを提供します.大きなネーミングスペースをuseする必要はありません.オブジェクトをインスタンス化する必要もありません.オブジェクトの具体的な方法にアクセスできます.
Facadeは実際にはコンテナ内のクラスの静的エージェントであり、コンテナ内の任意のオブジェクトに格納されている任意のメソッドを静的に呼び出すことができます.
クラスを定義してIlluminateSupportFacadesFacadeから継承し、抽象的なメソッドgetFacadeAccessorを実装すればよい.このメソッドは、文字列を返すだけで、サービスコンテナバインドクラスの別名を返します.実は、ソースコードで分かるように、オブジェクトを容器に入れる必要はなく、ここで直接返すこともできます.
AppFacadesでクラスCloudPiece.phpを作成

三、providersによるサービスプロバイダの登録すべてのサービスプロバイダがプロファイルapp.phpファイルのproviders配列に存在する
app->bind('CloudPiece', function()
        {
            return new CloudPiece;
        });
    }
}

四.Facadeの起動Facadeの起動ブートは、IlluminateFoundationBootstrapRegisterFacadesに登録されています.config/app.phpで対応するprovidersとaliasesを設定して、すべてのサービスプロバイダがプロファイルapp.phpファイルのproviders配列に参照および名前を付けます.
 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        App\Providers\CloudPieceServiceProvider::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [
        'CloudPiece' => App\Facades\CloudPiece::class,//    

    ],