To make requests to SMQ with the PHP SDK, you must configure an endpoint and provide access credentials. Choose a credential type based on the authentication and authorization requirements of your use case. This topic describes how to configure an endpoint, long-term access credentials, and temporary access credentials.
Configure the endpoint
When you use the PHP SDK to make requests to SMQ, you typically configure the endpoint at the bottom of the PHP file. As shown in the following example, set the endpoint in $endPoint = "".
$accessId = getenv(Constants::ALIYUN_AK_ENV_KEY);
$accessKey = getenv(Constants::ALIYUN_SK_ENV_KEY);
$endPoint = "";
You can find the SMQ endpoint on the Queue Details or Topic Details page of the console. To obtain the endpoint, log on to the Message Service (MNS) console. In the Queue List, click the name of your target queue to open the Queue Details page. In the Endpoint section, find the endpoint for either public or internal access. The address is in a format like http://{AccountId}.mns.cn-hangzhou.aliyuncs.com. Copy this value and assign it to the $endPoint variable.
Configure access credentials
You can use the following types of access credentials:
Long-term access credential: For security, we recommend that you use temporary access credentials instead of long-term ones. However, in development and testing environments where convenience is a priority, a long-term access credential avoids frequent credential rotation. To improve security, rotate your long-term access credentials every three months. If a long-term access credential is leaked or no longer needed, disable or delete it immediately to prevent security risks.
Temporary access credential: For high-security scenarios, such as temporarily authorizing an application to access SMQ, we recommend that you use temporary access credentials. A temporary access credential allows you to limit its validity period, which reduces the risk of credential leakage. In addition, temporary access credentials support permission control to effectively avoid the problem of excessive permissions.
Use long-term access credentials
When you need long-term access to the SMQ service in your application or service, you can access your SMQ by using the AccessKey pair of a RAM user.
-
Obtain the AccessKey pair for a RAM user.
For instructions, see Create an AccessKey pair for a RAM user.
ImportantWe recommend that you rotate any AccessKey pair older than three months. If an AccessKey pair is no longer in use, disable and delete it immediately.
-
Configure the AccessKey pair of the RAM user.
Environment variables
-
Configure the environment variables.
macOS
-
Open your terminal.
-
Run the following command.
nano ~/.bash_profile -
At the end of the file, add the AccessKey pair for your RAM user.
export ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** export ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs**** -
Press
Ctrl+X, pressYto confirm, and then pressEnterto save and exit. -
Run the following command to apply the changes.
source ~/.bash_profile -
Run the following commands to verify that the environment variables are set.
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRETA successful response looks like this:
LTA**** moiEs****
Linux
-
Open your terminal.
-
Run the following command.
sudo vim /etc/profile -
At the end of the file, add the AccessKey pair for your RAM user.
export ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** export ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs**** -
Press
ESCto exit edit mode, type:wq, and then pressEnterto save and exit the file. -
Run the following command to apply the changes.
source /etc/profile -
Run the following commands to verify that the environment variables are set.
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRETA successful response looks like this:
LTA**** moiEs****
Windows
GUI
These steps show how to set the AccessKey pair of a RAM user as environment variables by using the GUI in Windows 10:
-
On your desktop, right-click This PC and select Properties > Advanced system settings > Environment Variables. In either the User variables or System variables section, click New.
-
Add the following environment variables.
Parameter
Example value
ALIBABA_CLOUD_ACCESS_KEY_ID
LTA****
ALIBABA_CLOUD_ACCESS_KEY_SECRET
moiEs****
-
Run the following commands to verify that the environment variables are set.
echo %ALIBABA_CLOUD_ACCESS_KEY_ID% echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%A successful response looks like this:
LTA**** moiEs****
Command Prompt (CMD)
-
Open Command Prompt.
-
Run the following commands to set the AccessKey pair for the RAM user.
set ALIBABA_CLOUD_ACCESS_KEY_ID=LTA**** set ALIBABA_CLOUD_ACCESS_KEY_SECRET=moiEs**** -
To apply these settings to all new Command Prompt sessions, run the following commands.
setx ALIBABA_CLOUD_ACCESS_KEY_ID "%ALIBABA_CLOUD_ACCESS_KEY_ID%" setx ALIBABA_CLOUD_ACCESS_KEY_SECRET "%ALIBABA_CLOUD_ACCESS_KEY_SECRET%" -
Run the following commands to verify that the environment variables are set.
echo %ALIBABA_CLOUD_ACCESS_KEY_ID% echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%A successful response looks like this:
LTA**** moiEs****
Windows PowerShell
-
Press
Win + Xon your keyboard. -
Set the AccessKey pair for the RAM user.
Current session
-
From the menu that appears, select Windows PowerShell.
-
Add the following environment variables for the current session.
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "LTA****" $env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "moiEs****"
All new sessions
-
From the menu that appears, select Windows PowerShell.
-
Add the following environment variables for all new sessions.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::User)
All users
-
From the menu that appears, select Windows PowerShell (Admin).
-
Add the following environment variables for all users.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::Machine)
-
-
Run the following commands to verify that the environment variables are set.
Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRETA successful response looks like this:
LTA**** moiEs****
-
-
Obtain the AccessKey pair from the environment variables.
// Configure the access credentials by using the AccessKey pair obtained from the environment variables. $accessId = getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); $accessKey = getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
Embed in code
WarningHardcoding access credentials in your code poses a significant security risk. If your source code is exposed, attackers can access your SMQ resources. For enhanced security, we strongly recommend loading credentials from a secure source, such as environment variables, instead of embedding them in your code.
$accessId = getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); $accessKey = getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); $endPoint = "example.endpoint"; $client = new Client($endPoint, $accessId, $accessKey); -
Use temporary access credentials
For temporary access to the SMQ service, configure temporary access credentials. These credentials, obtained from Security Token Service (STS), allow secure access to SMQ without exposing your long-term AccessKey pair.
-
Create a RAM user.
For more information, see Create a RAM user.
-
Grant the
AliyunSTSAssumeRoleAccesspermission to the RAM user.For more information, see Grant permissions to a RAM user.
-
Use the RAM user to call the STS
AssumeRoleAPI operation to obtain temporary access credentials.For more information, see AssumeRole.
-
Configure the temporary access credentials.
Environment variables
-
Use the obtained temporary access credentials to configure environment variables.
macOS
-
Open your terminal.
-
Run the following command.
nano ~/.bash_profile -
At the end of the file, add the temporary access credentials obtained from STS: the AccessKey ID, AccessKey secret, and security token.
export MNS_ACCESS_KEY_ID=LTA**** export MNS_ACCESS_KEY_SECRET=moiEs**** export MNS_SESSION_TOKEN=CAES**** -
Press
Ctrl+X, pressYto confirm, and then pressEnterto save and exit. -
Run the following command to apply the changes.
source ~/.bash_profile -
Run the following commands to verify that the environment variables are set.
echo $MNS_ACCESS_KEY_ID echo $MNS_ACCESS_KEY_SECRET echo $MNS_SESSION_TOKENA successful response looks like this:
LTA**** moiEs**** CAES****
Linux
-
Open your terminal.
-
Run the following command.
sudo vim /etc/profile -
At the end of the file, add the temporary access credentials obtained from STS: the AccessKey ID, AccessKey secret, and security token.
export MNS_ACCESS_KEY_ID=LTA**** export MNS_ACCESS_KEY_SECRET=moiEs**** export MNS_SESSION_TOKEN=CAES**** -
Press
ESCto exit edit mode, type:wq, and then pressEnterto save and exit the file. -
Run the following command to apply the changes.
source /etc/profile -
Run the following commands to verify that the environment variables are set.
echo $MNS_ACCESS_KEY_ID echo $MNS_ACCESS_KEY_SECRET echo $MNS_SESSION_TOKENA successful response looks like this:
LTA**** moiEs**** CAES****
Windows
GUI
These steps show how to set the STS temporary access credentials as environment variables by using the GUI in Windows 10:
-
On your desktop, right-click This PC and select Properties > Advanced system settings > Environment Variables. In either the User variables or System variables section, click New.
-
Add the following environment variables.
Parameter
Example value
MNS_ACCESS_KEY_ID
LTA****
MNS_ACCESS_KEY_SECRET
moiEs****
MNS_SESSION_TOKEN
CAES****
-
Run the following commands to verify that the environment variables are set.
echo %MNS_ACCESS_KEY_ID% echo %MNS_ACCESS_KEY_SECRET% echo %MNS_SESSION_TOKEN%A successful response looks like this:
LTA**** moiEs**** CAES****
Command Prompt (CMD)
-
Open Command Prompt.
-
Run the following commands to set the temporary access credentials obtained from STS.
set MNS_ACCESS_KEY_ID=LTA**** set MNS_ACCESS_KEY_SECRET=moiEs**** set MNS_SESSION_TOKEN=CAES**** -
To apply these settings to all new Command Prompt sessions, run the following commands.
setx MNS_ACCESS_KEY_ID "%MNS_ACCESS_KEY_ID%" setx MNS_ACCESS_KEY_SECRET "%MNS_ACCESS_KEY_SECRET%" setx MNS_SESSION_TOKEN "%MNS_SESSION_TOKEN%" -
Run the following commands to verify that the environment variables are set.
echo %MNS_ACCESS_KEY_ID% echo %MNS_ACCESS_KEY_SECRET% echo %MNS_SESSION_TOKEN%A successful response looks like this:
LTA**** moiEs**** CAES****
Windows PowerShell
-
Press
Win + Xon your keyboard. -
Set the STS temporary access credentials.
Current session
-
From the menu that appears, select Windows PowerShell.
-
Add the following environment variables for the current session.
$env:MNS_ACCESS_KEY_ID = "LTA****" $env:MNS_ACCESS_KEY_SECRET = "moiEs****" $env:MNS_SESSION_TOKEN = "CAES****"
All new sessions
-
From the menu that appears, select Windows PowerShell.
-
Add the following environment variables for all new sessions.
[System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('MNS_SESSION_TOKEN', 'CAES****', [System.EnvironmentVariableTarget]::User)
All users
-
From the menu that appears, select Windows PowerShell (Admin).
-
Add the following environment variables for all users.
[System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_ID', 'LTA****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('MNS_ACCESS_KEY_SECRET', 'moiEs****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('MNS_SESSION_TOKEN', 'CAES****', [System.EnvironmentVariableTarget]::Machine)
-
-
Run the following commands to verify that the environment variables are set.
Get-ChildItem env:MNS_ACCESS_KEY_ID Get-ChildItem env:MNS_ACCESS_KEY_SECRET Get-ChildItem env:MNS_SESSION_TOKENA successful response looks like this:
LTA**** moiEs**** CAES****
-
-
Obtain the temporary access credentials from the environment variables.
$accessId = getenv("MNS_ACCESS_KEY_ID"); $accessKey = getenv("MNS_ACCESS_KEY_SECRET"); $securityToken = getenv("MNS_SESSION_TOKEN");
Embed in code
WarningHardcoding access credentials in your code poses a significant security risk. If your source code is exposed, attackers can access your SMQ resources. For enhanced security, we strongly recommend loading credentials from a secure source, such as environment variables, instead of embedding them in your code.
$accessId = getenv("MNS_ACCESS_KEY_ID"); $accessKey = getenv("MNS_ACCESS_KEY_SECRET"); $securityToken = getenv("MNS_SESSION_TOKEN"); $endPoint = "example.endpoint"; $client = new Client($endPoint, $accessId, $accessKey, $securityToken); -