I am trying to embed the Google+ username on my website and am encountering a problem. Here is my code (after creating the Google_Client object):
try { $client->authenticate($_GET['code']); $plus = new \Google_Service_Plus($client); $person = $plus->people->get('me'); $firstName = $person->modelData->name->givenName; } catch (Google_Auth_Exception $e) { $response = array( 'error' => 'Error: Authentication exception.' ); } catch (Exception $e) { $response = array( 'error' => 'Error: Uncaught exception.' ); }
$client->authenticate() throws a Google_Auth_Exception if the code passed to it is invalid- If authentication fails, then reading the properties of the
$person object causes fatal errors $response is called as a JSON encoded object
The problem is that the try/catch code is not working properly. When authentication failed due to incorrect code in $_GET['code'] , from the script:
The following answer is returned:
{"error":"Error: Authentication exception."}
So far, so good - the code in the first catch block has been executed.
However, the code in the try block continues to execute in a strange way. I say “weird” because in the form above there are a lot of errors (culminating in a fatal error), which means this line:
$firstName = $person->modelData->name->givenName;
Still running! It should not be executed because the exception was thrown on the previous line. If I comment on the above line, errors will not be thrown (again indicating that this line has been completed, which should not be).
Here are the errors that were thrown due to the above line that is executed after the exception:
Note: Undefined index: modelData in [...] \ google-api-php-client-master \ src \ Google \ Model.php on line 78
Note. Trying to get a non-object property in [...] \ ajax_handler.php on line 720 [note: this is the line shown above where the object is accessed]
Note: attempt to get non-object property in [...] \ ajax_handler.php on line 720
Another reason I said “weird” is because if I add this line:
die('dying before reading property');
Right before the specified line (where I read the property), no errors occur, BUT the text "skill to read" is not displayed on the page! This is strange because the script obviously still executes the code in the try block after an error occurs (since without this die () line, the line reading the property is executed and leads to a large number of errors being printed). As before, the code in the catch block is still executing, and JSON is output to the page.
What's happening?