访问Google_Service_Exception中的属性时出现问题

时间:2016-07-19 09:28:52

标签: php google-api google-apps google-api-php-client

我正在学习如何使用Google Reseller API的教程。我已经来确定客户是否已经存在于Google Apps中(第2步)但是在处理Google_Service_Exception对象时已经失败了。

如果客户不存在,那么对API的调用将返回404错误。我使用code对象Google_Service_Exception的{​​{1}}属性来确定响应是否有404错误。但是当我尝试使用$e返回错误代码时使用:

$e->code

我收到以下PHP错误:

  

致命错误:无法访问受保护的属性Google_Service_Exception :: $ code。

Google_Service_Exception类如下:

try {
 // Call to the Google Reseller API
} catch (Google_Service_Exception $e) {
  if($e->code == 404){
    return false;
  }
}

所以我认为错误与<?php require_once 'Google/Exception.php'; class Google_Service_Exception extends Google_Exception { /** * Optional list of errors returned in a JSON body of an HTTP error response. */ protected $errors = array(); /** * Override default constructor to add ability to set $errors. * * @param string $message * @param int $code * @param Exception|null $previous * @param [{string, string}] errors List of errors returned in an HTTP * response. Defaults to []. */ public function __construct( $message, $code = 0, Exception $previous = null, $errors = array() ) { if (version_compare(PHP_VERSION, '5.3.0') >= 0) { parent::__construct($message, $code, $previous); } else { parent::__construct($message, $code); } $this->errors = $errors; } /** * An example of the possible errors returned. * * { * "domain": "global", * "reason": "authError", * "message": "Invalid Credentials", * "locationType": "header", * "location": "Authorization", * } * * @return [{string, string}] List of errors return in an HTTP response or []. */ public function getErrors() { return $this->errors; } } 受保护的事实有关。我想它受到保护是有原因所以我有点担心改变课程。任何帮助/指针解决此错误将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

只需使用getCode()方法:

try {
     // Call to the Google Reseller API
} catch (Google_Service_Exception $e) {
     if($e->getCode() == 404){ // <- Change is here
         return false;
     }
}

Google_Service_Exception延伸Google_ExceptionGoogle_Exception延伸Exception。您可以在此处阅读documentation about Exception。您将看到getCode方法。

相关问题