Why code work in PHP but not in SC

hi,
I have this code which when run in a standard php file works perfectly (IN SC ENVIRONMENT) but when put in a blank scriptcase application it returns an error :
Parse error: syntax error, unexpected token “use” in C:\Program Files\NetMake\v9-php81\wwwroot\scriptcase\app\CediOnlineSiziano\push_v4\index.php on line 2348

code:
require_once ‘…/_lib/libraries/sys/php-jwt/src/JWT.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/JWTExceptionWithPayloadInterface.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/BeforeValidException.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/ExpiredException.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/SignatureInvalidException.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/JWK.php’;
require_once ‘…/_lib/libraries/sys/php-jwt/src/Key.php’;

// Carica le credenziali dell’account di servizio
$serviceAccount = json_decode(file_get_contents(’…/_lib/libraries/sys/php-jwt/src/xxxxxxxxx.json’), true);

use \Firebase\JWT\JWT; // ERROR IN SCRIPTACASE

$now = time();

$token = [
‘iss’ => $serviceAccount[‘client_email’],
‘aud’ => ‘https://oauth2.googleapis.com/token’,
‘iat’ => $now,
‘exp’ => $now + 3600, // 1 ora di validità
‘scope’ => ‘https://www.googleapis.com/auth/firebase.messaging’ // Cambia lo scope in base alle tue esigenze
];

// Genera il JWT
$jwt = JWT::encode($token, $serviceAccount[‘private_key’], ‘RS256’);

//echo "JWT: " . $jwt . “\n”;

// Richiedi il token di accesso utilizzando il JWT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://oauth2.googleapis.com/token’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
‘grant_type’ => ‘urn:ietf:params:oauth:grant-type:jwt-bearer’,
‘assertion’ => $jwt
]));

$response = curl_exec($ch);
curl_close($ch);

$responseData = json_decode($response, true);
$accessToken = $responseData[‘access_token’]; // Il token di accesso ottenuto dal passaggio precedente
//echo $accessToken;

$notification = [
‘message’ => [
‘token’ => ‘xxxxxxxxxxx’ // Sostituisci con il token del dispositivo di destinazione
‘notification’ => [
‘title’ => ‘Titolo della Notifica’,
‘body’ => ‘Corpo della Notifica’
]
]
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://fcm.googleapis.com/v1/projects/toccate-96934/messages:send’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ’ . $accessToken,
‘Content-Type: application/json’
]);

$response = curl_exec($ch);
curl_close($ch);

$responseData = json_decode($response, true);
print_r($responseData);

In a blank application, it’s a member in a class, you cannot use the

use \Firebase\JWT\JWT; 

so when you have to call a class or a member of the included library you have to write the complete path of the member so

// Genera il JWT
$jwt = \Firebase\JWT\JWT::encode($token, $serviceAccount[‘private_key’], ‘RS256’);

bye

Thanks! now it works perfectly!!!

1 Like