private static function getAuthenticationCode(Client $client) { // TODO: Figure out why the callback URL (passed as the second // parameter) does not seem to matter here, but passing a non-empty // string causes the authentication process to not be initiated. $url = $client->getLoginUrl(array('wl.skydrive_update'), ''); echo "Integration test suite started.\n\nPlease sign into your OneDrive account from this page and grant to the app all\nthe privileges requested:\n\n\t{$url}\n\nThis process will then resume, do not interrupt it.\n"; $server = @socket_create_listen(self::PORT, 1); if (false === $server) { $message = socket_strerror(socket_last_error()); throw new \Exception($message); } $socketRemote = @socket_accept($server); if (false === $socketRemote) { $message = socket_strerror(socket_last_error()); socket_close($server); throw new \Exception($message); } $buffer = @socket_read($socketRemote, 4096, PHP_BINARY_READ); if (false === $buffer) { $message = socket_strerror(socket_last_error()); socket_close($socketRemote); socket_close($server); throw new \Exception($message); } $size = @socket_write($socketRemote, implode("\r\n", array('HTTP/1.1 200 OK', 'Content-Type: text/html; charset=utf-8', '', '<!DOCTYPE html><h1>Thank you</h1><p>The integration test suite started running. You can close this window.</p>'))); if (false === $size) { $message = socket_strerror(socket_last_error()); socket_close($socketRemote); socket_close($server); throw new \Exception($message); } socket_close($socketRemote); socket_close($server); list($headers, $body) = explode("\r\n\r\n", $buffer); $headers = explode("\r\n", $headers); $request = $headers[0]; if (1 !== preg_match('/^GET\\s+(.+)\\s+HTTP\\/1\\.1$/', $request, $matches)) { throw new \Exception('Unsupported HTTP request format'); } $url = $matches[1]; $components = parse_url($url); $query = $components['query']; $params = explode('&', $query); $query = array(); array_map(function ($param) use(&$query) { list($key, $value) = explode('=', $param); $query[$key] = $value; }, $params); if (!array_key_exists('code', $query)) { throw new \Exception('Code is missing from the request. Did you log in successfully and granted all the privileges requested?'); } return $query['code']; }