Mam kod jak poniżej. Kiedy wprowadzę w zmienną refreshtoken ręcznie np: 'refreshtoken' => 'abc' wszystko jest ok. Ale jeśli 'refreshtoken' => $plik wywala błąd:
Kod
[Fatal error: Constant expression contains invalid operations in /webapi/oauth.php on line 10
Plik zawiera zwykły ciąg znaków.
Dlaczego i jak to poprawić?
Kod skryptu:
<?php /** * @author AlleREST.pl * @link <a href="http://allerest.pl/logowanie-do-allegro-rest-api/" target="_blank">http://allerest.pl/logowanie-do-allegro-rest-api/</a> */ class AllegroOAuth2Client { protected $providerSettings = [ 'ClientId' => 'xxx', 'ClientSecret' => 'aaa', 'ApiKey' => 'bbb', 'RedirectUri' => 'ccc', 'AuthorizationUri' => 'https://allegro.pl/auth/oauth/authorize', 'TokenUri' => 'https://allegro.pl/auth/oauth/token', 'refreshtoken' => $plik ]; protected $headers = [ 'Content-Type: application/x-www-form-urlencoded' ]; $this->headers[] = 'Authorization: Basic '. base64_encode($this->providerSettings['ClientId'] . ':' . $this->providerSettings['ClientSecret']); } public function tokenRequest($code) { $curl = curl_init($this->providerSettings['TokenUri']); curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query([ 'grant_type' => 'refresh_token', 'refresh_token' => $plik, 'redirect_uri' => $this->providerSettings['RedirectUri'], 'api_key' => $this->providerSettings['ApiKey'] ])); $result = ($result = curl_exec($curl)) === false ? false : json_decode($result); if ($result === false) { throw new Exception('Unrecognized error'); throw new Exception($result->error . ' - ' . $result->error_description); } else { return $result; } } public function getAuthorizationUri() { return $this->providerSettings['AuthorizationUri'] . '?' . http_build_query([ 'response_type' => 'code', 'client_id' => $this->providerSettings['ClientId'], 'api_key' => $this->providerSettings['ApiKey'], 'redirect_uri' => $this->providerSettings['RedirectUri'] ]); } } // Indywidualne parametry można podać do konstruktora w postaci tablicy, lub bezpośrednio w definicji klasy. $auth = new AllegroOAuth2Client(); // Dobrze byłoby sprawdzić od kogo przychodzi żądanie try { $result = $auth->tokenRequest($_GET['code']); file_put_contents('.verkey', $result->refresh_token); } catch(Exception $e) { } } else { } ?>