Skip to main content

Installing PHP SDK for Data Storage and Instant Messaging

info

The PHP SDK is a server-side SDK primarily used to invoke management interfaces and cloud functions in trusted server environments like a cloud engine. If you need to use the PHP SDK on your own server, please refer to this documentation for the configuration process.

Installing SDK

There are several ways for you to install our SDK and the most convenient one is to use a package manager.

Installing with Package Manager

Composer is a recommended PHP package management tool. To install the SDK, simply execute the following command:

composer require leancloud/leancloud-sdk

Installing Manually

PHP SDK

Initializing Your Project

You can view the credentials of your app by going to Dashboard > Settings > App keys:

  • App ID will be used when you initialize the SDK.
  • App Key will be used when you initialize the SDK on the client side.
  • Master Key will be used when you interact with admin interfaces on trusted environments including your own server and LeanEngine. When you use it, all the permission checks will be skipped. Make sure to keep it private and never use it in the code for the client.

See Domain for setting up the domain.

Then import Client and call initialize to initialize it:

use \LeanCloud\Client;
// 参数依次为 App ID、App Key、Master Key
Client::initialize("your-app-id", "your-app-key", "your-master-key");

Enabling Debug Logs

You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to IDE, browser console, or your app's Dashboard > LeanEngine > App logs.

// Place the line after Client::initialize(); only needs to be called once
Client::setDebug(true)

Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.

Verifying

First of all, make sure you are able to connect to LeanCloud server. You can test it by running the following command:

curl "https://{{host}}/1.1/date"

If everything goes well, it will return the current date:

{ "__type": "Date", "iso": "2020-10-12T06:46:56.000Z" }

Then add the following code into your project:

$testObject = new LeanObject("TestObject");
$testObject->set("words", "Hello world!");
try {
$testObject->save();
echo "Object saved.";
} catch (Exception $ex) {
echo "An error occurred.";
}

Save and run your program.

Then go to Dashboard > Data Storage > Data > TestObject. If you see a row with its words column being Hello world!, it means that you have correctly installed the SDK.

See Debugging if you’re not seeing the content.

Debugging

This guide is written for the latest version of our SDK. If you encounter any error, please first make sure you have the latest version installed.

401 Unauthorized

If you get a 401 error or see the following content in network logs:

{
"code": 401,
"error": "Unauthorized."
}

It means that the App ID or App Key might be incorrect or don't match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such error.

Client Cannot Access the Internet

Make sure you have granted enough permissions for your mobile app.