Home >Backend Development >PHP Tutorial >How to use try_catch in yii2-wx

How to use try_catch in yii2-wx

不言
不言Original
2018-07-10 14:52:531970browse

This article mainly introduces how to use try_catch in yii2-wx. It has certain reference value. Now I share it with you. Friends in need can refer to it.

As for why you should use try.. I don’t want to say more about the .catch... structure. This post will talk about how to use it in yii2-wx.

In other words, how many Exceptions do we need to pay attention to in yii2-wx? As we all know, yii2-wx is a communication library. Basically, every method has the operation of initiating a request with the WeChat server. To simulate the http client, we use the official extension of yii, yii2-httpclient.

So when we use yii2-wx, the first step to catch is the client exception, the following code

try {
    $qrcodeFile = $qrcode->strTemp(300,$code);
}catch(\yii\httpclient\Exception $e){
    // todo
}

This is where we have to catch the yiihttpclientException exception in communication.

Next let’s talk about the deconstruction of yii2-wx. As a single-entry extension, the Application class is very important, and this class also has exceptions thrown, such as when you use the driver to drive an interface helper object. When, the name of the interface is incorrect, this throws the Exception built in yii2-wx, so the correct way to use the driver is as follows

try {
    $app = new Application(['conf'=>$conf['mp']]);    
    $qrcode = $app->driver("mp.qrcode");
}catch(\abei2017\wx\core\Exception $e){
    // todo
}

The above mentioned the overall framework, and then the specific method, for example, when we use When $qrcode->strTemp(300,$code), there is no problem with communication, but there is a problem with obtaining the QR code result. At this time, these assistant methods will also throw an abei2017wxcoreException exception, which can be found in $e->getMessage Get the information in ().

Finally, let’s summarize the complete yii2-wx code writing structure.

try {
    $app = new Application(['conf'=>$conf['mp']]);
    $qrcode = $app->driver("mp.qrcode");
    $code = Yii::$app->security->generateRandomString();
    $qrcodeFile = $qrcode->strTemp(300,$code);
    
}catch(\abei2017\wx\core\Exception $e){
    // todo
}catch(\yii\httpclient\Exception $e){
    // todo
}

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the usage summary of GridView in Yii2

About how to call phantomjs in php to share with WeChat applet The problem

The above is the detailed content of How to use try_catch in yii2-wx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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