Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Proste pobranie danych z pliku - problem.
kggsm
post 30.11.2018, 23:01:44
Post #1





Grupa: Zarejestrowani
Postów: 15
Pomógł: 0
Dołączył: 31.12.2008

Ostrzeżenie: (0%)
-----


Witam.
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:
  1. <?php
  2. /**
  3.  * @author AlleREST.pl
  4.  * @link <a href="http://allerest.pl/logowanie-do-allegro-rest-api/" target="_blank">http://allerest.pl/logowanie-do-allegro-rest-api/</a>
  5.  */
  6. $plik = file_get_contents('./.verkey', FILE_USE_INCLUDE_PATH);
  7. echo 'Twój Refresh Token: ' . $plik . '';
  8. class AllegroOAuth2Client {
  9.  
  10. protected $providerSettings = [
  11. 'ClientId' => 'xxx',
  12. 'ClientSecret' => 'aaa',
  13. 'ApiKey' => 'bbb',
  14. 'RedirectUri' => 'ccc',
  15.  
  16. 'AuthorizationUri' => 'https://allegro.pl/auth/oauth/authorize',
  17. 'TokenUri' => 'https://allegro.pl/auth/oauth/token',
  18. 'refreshtoken' => $plik
  19. ];
  20. protected $headers = [
  21. 'Content-Type: application/x-www-form-urlencoded'
  22. ];
  23.  
  24. public function __construct(array $customSettings = []) {
  25. $this->providerSettings = array_merge($this->providerSettings, $customSettings);
  26. $this->headers[] = 'Authorization: Basic '. base64_encode($this->providerSettings['ClientId'] . ':' . $this->providerSettings['ClientSecret']);
  27. }
  28.  
  29. public function tokenRequest($code) {
  30. $curl = curl_init($this->providerSettings['TokenUri']);
  31.  
  32. curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
  33. curl_setopt($curl, CURLOPT_POST, true);
  34. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  35.  
  36. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query([
  37. 'grant_type' => 'refresh_token',
  38. 'refresh_token' => $plik,
  39. 'redirect_uri' => $this->providerSettings['RedirectUri'],
  40. 'api_key' => $this->providerSettings['ApiKey']
  41. ]));
  42.  
  43. $result = ($result = curl_exec($curl)) === false ? false : json_decode($result);
  44.  
  45. if ($result === false) {
  46. throw new Exception('Unrecognized error');
  47. } else if (!empty($result->error)) {
  48. throw new Exception($result->error . ' - ' . $result->error_description);
  49. } else {
  50. return $result;
  51. }
  52. }
  53.  
  54. public function getAuthorizationUri() {
  55. return $this->providerSettings['AuthorizationUri'] . '?' . http_build_query([
  56. 'response_type' => 'code',
  57. 'client_id' => $this->providerSettings['ClientId'],
  58. 'api_key' => $this->providerSettings['ApiKey'],
  59. 'redirect_uri' => $this->providerSettings['RedirectUri']
  60. ]);
  61. }
  62. }
  63.  
  64. // Indywidualne parametry można podać do konstruktora w postaci tablicy, lub bezpośrednio w definicji klasy.
  65. $auth = new AllegroOAuth2Client();
  66.  
  67. // Dobrze byłoby sprawdzić od kogo przychodzi żądanie
  68. if (!empty($_GET['code'])) {
  69. try
  70. {
  71. $result = $auth->tokenRequest($_GET['code']);
  72.  
  73.  
  74. echo 'Twój Access Token: ' . $result->access_token . '';
  75. echo '<br>Twój Refresh Token: ' . $result->refresh_token . '';
  76. echo '<br>Twój expires_in: ' . $result->expires_in . '';
  77.  
  78. file_put_contents('.verkey', $result->refresh_token);
  79.  
  80.  
  81.  
  82. }
  83. catch(Exception $e)
  84. {
  85. echo $e->getMessage();
  86. }
  87. } else {
  88. echo ' Logowanie: <a href="'.$auth->getAuthorizationUri().'">Zaloguj do Allegro</a>';
  89. }
  90.  
  91.  
  92. ?>
Go to the top of the page
+Quote Post
Tomplus
post 1.12.2018, 08:21:02
Post #2





Grupa: Zarejestrowani
Postów: 1 828
Pomógł: 225
Dołączył: 20.03.2005
Skąd: Będzin

Ostrzeżenie: (0%)
-----


Zauważ co masz w $providerSettings

Gdy deklarujesz własność wrzucasz do niej zmienną $plik. Więc może sprawdź najpierw swoją klasę bo ta zmienna jest w kilku miejscach i nawet nie ma nic do niej. Chyba że faktycznie to jest jakieś $_GET['plik'] - ale to nic nie zmienia.

Go to the top of the page
+Quote Post
Pilsener
post 1.12.2018, 12:31:36
Post #3





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


Problem ze składnią. Jak masz klasę, to kod się pisze wewnątrz klasy a nie przed nią (zasięg zmiennych się kłania: http://php.net/manual/en/language.variables.scope.php), poza tym jak pisać właściwość klasy: http://php.net/manual/en/language.oop5.properties.php
Reasumując: próbujesz użyć nieistniejącej zmiennej w niedozwolony sposób i dlatego jest fatal.
Go to the top of the page
+Quote Post
Pyton_000
post 1.12.2018, 13:30:02
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Przeczytaj implementację konstruktora klasy `AllegroOAuth2Client` to może Cię olśni.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 10:51