PHP code example of leancloud / leancloud-sdk

1. Go to this page and download the library: Download leancloud/leancloud-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leancloud / leancloud-sdk example snippets


// 如果是 composer 安装
// re_once("vendor/leancloud/src/autoload.php");

// 参数依次为 app-id, app-key, master-key
LeanCloud\Client::initialize("app_id", "app_key", "master_key");

use LeanCloud\User;
use LeanCloud\CloudException;

$user = new User();
$user->setUsername("alice");
$user->setEmail("[email protected]");
$user->setPassword("passpass");
try {
    $user->signUp();
} catch (CloudException $ex) {
    // 如果 LeanCloud 返回错误,这里会抛出异常 CloudException
    // 如用户名已经被注册:202 Username has been taken
}

// 注册成功后,用户被自动登录。可以通过以下方法拿到当前登录用户和
// 授权码。
User::getCurrentUser();
User::getCurrentSessionToken();

User::logIn("alice", "passpass");
$user = User::getCurrentUser();
$token = User::getCurrentSessionToken();

// 给定一个 token 可以很容易的拿到用户
User::become($token);

// 我们还支持短信验证码,及第三方授权码登录
User::logInWithSmsCode("phone number", "sms code");
User::logInWith("weibo", array("openid" => "..."));

use LeanCloud\LeanObject;
use LeanCloud\CloudException;

$obj = new LeanObject("TestObject");
$obj->set("name", "alice");
$obj->set("height", 60.0);
$obj->set("weight", 4.5);
$obj->set("birthdate", new \DateTime());
try {
    $obj->save();
} catch (CloudException $ex) {
    // CloudException 会被抛出,如果保存失败
}

// 获取字段值
$obj->get("name");
$obj->get("height");
$obj->get("birthdate");

// 原子增加一个数
$obj->increment("age", 1);

// 在数组字段中添加,添加唯一,删除
// 注意: 由于API限制,不同数组操作之间必须保存,否则会报错
$obj->addIn("colors", "blue");
$obj->save();
$obj->addUniqueIn("colors", "orange");
$obj->save();
$obj->removeIn("colors", "blue");
$obj->save();

// 在云存储上删除数据
$obj->destroy();

class TestObject extends LeanObject {
    protected static $className = "TestObject";
    public setName($name) {
        $this->set("name", $name);
        return $this;
    }
}
// register it as storage class
TestObject::registerClass();

$obj = new TestObject();
$obj->setName();
$obj->set("eyeColor", "blue");
...

use LeanCloud\Query;

$query = new Query("TestObject");
$obj = $query->get($objectId);

$query = new Query("TestObject");
$query->lessThan("height", 100.0);           // 小于
$query->greaterThanOrEqualTo("weight", 5.0); // 大于等于
$query->addAscend("birthdate");              // 递增排序
$query->addDescend("name");                  // 递减排序
$query->count();
$query->first(); // 返回第一个对象

$query->skip(100);
$query->limit(20);
$objects = $query->find(); // 返回查询到的对象

use LeanCloud\File;
$file = File::createWithData("hello.txt", "Hello LeanCloud!");
try {
    $file->save();
} catch (CloudException $ex) {
    // 云存储返回错误,保存失败
}

$file->getSize();
$file->getName();
$file->getUrl();

$file = File::createWithLocalFile("/tmp/myfile.png");
try {
    $file->save();
} catch (CloudException $ex) {
    // 云存储返回错误,保存失败
}

// 获取文件缩略图的链接
$url = $file->getThumbUrl();

$file = File::createWithUrl("image.png", "http://example.net/image.png");
try {
    $file->save();
} catch (CloudException $ex) {
    // 云存储返回错误,保存失败
}
bash
cd $APP_ROOT
wget https://github.com/leancloud/php-sdk/archive/vX.X.X.zip

# 解压并置于 vendor 目录
unzip vX.X.X.zip
mv php-sdk-X.X.X vendor/leancloud