PFinal\Wechat\Support\Json::parseOrFail PHP Method

parseOrFail() public static method

解析微信平台返回的json字符串,转为数组,错误时,抛异常
public static parseOrFail ( $jsonStr ) : array
$jsonStr
return array
    public static function parseOrFail($jsonStr)
    {
        $arr = json_decode($jsonStr, true);
        if (isset($arr['errcode']) && 0 !== $arr['errcode']) {
            if (empty($arr['errmsg'])) {
                $arr['errmsg'] = 'Unknown';
            }
            throw new WechatException($arr['errmsg'], $arr['errcode']);
        }
        return $arr;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * 请求微信平台服务器,并解析返回的json字符串为数组,失败抛异常
  * @param $url
  * @param $data
  * @return array
  * @throws \PFinal\Wechat\WechatException
  */
 protected static function request($url, $data = null, $jsonEncode = true)
 {
     $executeUrl = str_replace('ACCESS_TOKEN', self::getApi()->getAccessToken(), $url);
     if ($jsonEncode) {
         $data = Json::encode($data);
     }
     try {
         return Json::parseOrFail(Curl::execute($executeUrl, is_null($data) ? 'get' : 'post', $data));
     } catch (WechatException $ex) {
         //更新AccessToken再次请求
         if ($ex->getCode() == 40001) {
             $executeUrl = str_replace('ACCESS_TOKEN', self::getApi()->getAccessToken(false), $url);
             return Json::parseOrFail(Curl::execute($executeUrl, is_null($data) ? 'get' : 'post', $data));
         }
         throw $ex;
     }
 }
All Usage Examples Of PFinal\Wechat\Support\Json::parseOrFail