In developing web applications that interact with Facebook, the issue of getUser() consistently returning 0 is commonly encountered. This can hinder access to necessary user information and result in authentication errors.
One potential reason for this behavior lies in the PHP SDK's reliance on the $_REQUEST variable, which may not be merging correctly with other request variables ($_GET, $_POST, and $_COOKIE) in your environment. The PHP version can also play a role.
Inspecting the getCode() function within the base_facebook.php file reveals the following code:
protected function getCode() { if (isset($_REQUEST['code'])) { if ($this->state !== null && isset($_REQUEST['state']) && $this->state === $_REQUEST['state']) { // CSRF state has done its job, so clear it $this->state = null; $this->clearPersistentData('state'); return $_REQUEST['code']; } else { self::errorLog('CSRF state token does not match one provided.'); return false; } } return false; }
To address this issue, we can modify this function as follows:
protected function getCode() { $server_info = array_merge($_GET, $_POST, $_COOKIE); if (isset($server_info['code'])) { if ($this->state !== null && isset($server_info['state']) && $this->state === $server_info['state']) { // CSRF state has done its job, so clear it $this->state = null; $this->clearPersistentData('state'); return $server_info['code']; } else { self::errorLog('CSRF state token does not match one provided.'); return false; } } return false; }
This modification consolidates all request variables into the $server_info variable, which ensures that the getCode() function can access the code parameter from any of these sources, regardless of your PHP environment's configuration.
The above is the detailed content of Why Does Facebook\'s PHP SDK `getUser()` Always Return 0?. For more information, please follow other related articles on the PHP Chinese website!