{"id":19614772,"url":"https://github.com/leancloud/php-sdk","last_synced_at":"2025-04-09T20:10:33.210Z","repository":{"id":44729758,"uuid":"42485979","full_name":"leancloud/php-sdk","owner":"leancloud","description":"LeanCloud PHP SDK","archived":false,"fork":false,"pushed_at":"2024-05-11T08:57:40.000Z","size":1394,"stargazers_count":53,"open_issues_count":33,"forks_count":23,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-09T20:10:30.420Z","etag":null,"topics":["leancloud","leanengine","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leancloud.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-15T00:57:54.000Z","updated_at":"2024-05-11T08:56:07.000Z","dependencies_parsed_at":"2024-06-18T16:29:29.656Z","dependency_job_id":"0ff62337-68d8-42f6-8787-82a939d4e021","html_url":"https://github.com/leancloud/php-sdk","commit_stats":{"total_commits":384,"total_committers":8,"mean_commits":48.0,"dds":"0.25260416666666663","last_synced_commit":"66f57f0132c83bd91bd13d8ebdabb7d977cf5095"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fphp-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fphp-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fphp-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fphp-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leancloud","download_url":"https://codeload.github.com/leancloud/php-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leancloud","leanengine","php"],"created_at":"2024-11-11T10:53:42.136Z","updated_at":"2025-04-09T20:10:33.189Z","avatar_url":"https://github.com/leancloud.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"LeanCloud PHP SDK\n====\n\n[![Build Status](https://img.shields.io/travis/leancloud/php-sdk.svg)\n](https://travis-ci.org/leancloud/php-sdk)\n[![Latest Version](https://img.shields.io/packagist/v/leancloud/leancloud-sdk.svg)\n](https://packagist.org/packages/leancloud/leancloud-sdk)\n[![Coverage Status](https://img.shields.io/codecov/c/github/leancloud/php-sdk/master.svg)](https://codecov.io/github/leancloud/php-sdk)\n\nLeanCloud 为应用提供了从数据存储，消息推送，实时通信到离线分析等全方位\n的一站式云端服务，帮助应用开发者降低后端开发及维护成本，为应用开发加速。\nPHP SDK 提供了对数据存储，用户管理等模块的 PHP 实现及接口，以方便 PHP\n应用的开发。\n\n安装\n----\n\n运行环境要求 PHP 5.6 及以上版本，以及\n[cURL](http://php.net/manual/zh/book.curl.php)。\n\n#### composer 安装\n\n如果使用标准的包管理器 composer，你可以很容易的在项目中添加依赖并下载：\n\n```bash\ncomposer require leancloud/leancloud-sdk\n```\n\n#### 手动下载安装\n\n你也可以前往[发布页面](https://github.com/leancloud/php-sdk/releases)\n手动下载安装包。假设你的应用位于 `$APP_ROOT` 目录下：\n\n```bash\ncd $APP_ROOT\nwget https://github.com/leancloud/php-sdk/archive/vX.X.X.zip\n\n# 解压并置于 vendor 目录\nunzip vX.X.X.zip\nmv php-sdk-X.X.X vendor/leancloud\n```\n\n初始化\n----\n\n完成上述安装后，需要对 SDK 初始化。如果已经创建应用，可以在 LeanCloud\n[**控制台** \u003e **应用设置**]里找到应用的 ID 和 key。然后在项目中加载 SDK，\n并初始化：\n\n```php\n// 如果是 composer 安装\n// require_once(\"vendor/autoload.php\");\n\n// 如果是手动安装\nrequire_once(\"vendor/leancloud/src/autoload.php\");\n\n// 参数依次为 app-id, app-key, master-key\nLeanCloud\\Client::initialize(\"app_id\", \"app_key\", \"master_key\");\n```\n\n使用示例\n----\n\n#### 用户注册及管理\n\n注册一个用户:\n\n```php\nuse LeanCloud\\User;\nuse LeanCloud\\CloudException;\n\n$user = new User();\n$user-\u003esetUsername(\"alice\");\n$user-\u003esetEmail(\"alice@example.net\");\n$user-\u003esetPassword(\"passpass\");\ntry {\n    $user-\u003esignUp();\n} catch (CloudException $ex) {\n    // 如果 LeanCloud 返回错误，这里会抛出异常 CloudException\n    // 如用户名已经被注册：202 Username has been taken\n}\n\n// 注册成功后，用户被自动登录。可以通过以下方法拿到当前登录用户和\n// 授权码。\nUser::getCurrentUser();\nUser::getCurrentSessionToken();\n```\n\n登录一个用户:\n\n```php\nUser::logIn(\"alice\", \"passpass\");\n$user = User::getCurrentUser();\n$token = User::getCurrentSessionToken();\n\n// 给定一个 token 可以很容易的拿到用户\nUser::become($token);\n\n// 我们还支持短信验证码，及第三方授权码登录\nUser::logInWithSmsCode(\"phone number\", \"sms code\");\nUser::logInWith(\"weibo\", array(\"openid\" =\u003e \"...\"));\n```\n\n#### 对象存储\n\n```php\nuse LeanCloud\\LeanObject;\nuse LeanCloud\\CloudException;\n\n$obj = new LeanObject(\"TestObject\");\n$obj-\u003eset(\"name\", \"alice\");\n$obj-\u003eset(\"height\", 60.0);\n$obj-\u003eset(\"weight\", 4.5);\n$obj-\u003eset(\"birthdate\", new \\DateTime());\ntry {\n    $obj-\u003esave();\n} catch (CloudException $ex) {\n    // CloudException 会被抛出，如果保存失败\n}\n\n// 获取字段值\n$obj-\u003eget(\"name\");\n$obj-\u003eget(\"height\");\n$obj-\u003eget(\"birthdate\");\n\n// 原子增加一个数\n$obj-\u003eincrement(\"age\", 1);\n\n// 在数组字段中添加，添加唯一，删除\n// 注意: 由于API限制，不同数组操作之间必须保存，否则会报错\n$obj-\u003eaddIn(\"colors\", \"blue\");\n$obj-\u003esave();\n$obj-\u003eaddUniqueIn(\"colors\", \"orange\");\n$obj-\u003esave();\n$obj-\u003eremoveIn(\"colors\", \"blue\");\n$obj-\u003esave();\n\n// 在云存储上删除数据\n$obj-\u003edestroy();\n```\n\n我们同样支持子类继承，子类中需要定义静态变量 `$className` ，并注册到存储类:\n\n```php\nclass TestObject extends LeanObject {\n    protected static $className = \"TestObject\";\n    public setName($name) {\n        $this-\u003eset(\"name\", $name);\n        return $this;\n    }\n}\n// register it as storage class\nTestObject::registerClass();\n\n$obj = new TestObject();\n$obj-\u003esetName();\n$obj-\u003eset(\"eyeColor\", \"blue\");\n...\n```\n\n#### 对象查询\n\n给定一个 objectId，可以如下获取对象。\n\n```php\nuse LeanCloud\\Query;\n\n$query = new Query(\"TestObject\");\n$obj = $query-\u003eget($objectId);\n```\n\n更为复杂的条件查询：\n\n```php\n$query = new Query(\"TestObject\");\n$query-\u003elessThan(\"height\", 100.0);           // 小于\n$query-\u003egreaterThanOrEqualTo(\"weight\", 5.0); // 大于等于\n$query-\u003eaddAscend(\"birthdate\");              // 递增排序\n$query-\u003eaddDescend(\"name\");                  // 递减排序\n$query-\u003ecount();\n$query-\u003efirst(); // 返回第一个对象\n\n$query-\u003eskip(100);\n$query-\u003elimit(20);\n$objects = $query-\u003efind(); // 返回查询到的对象\n```\n\n#### 文件存储\n\n直接创建文件:\n\n```php\nuse LeanCloud\\File;\n$file = File::createWithData(\"hello.txt\", \"Hello LeanCloud!\");\ntry {\n    $file-\u003esave();\n} catch (CloudException $ex) {\n    // 云存储返回错误，保存失败\n}\n\n$file-\u003egetSize();\n$file-\u003egetName();\n$file-\u003egetUrl();\n```\n\n由本地文件创建：\n\n```php\n$file = File::createWithLocalFile(\"/tmp/myfile.png\");\ntry {\n    $file-\u003esave();\n} catch (CloudException $ex) {\n    // 云存储返回错误，保存失败\n}\n\n// 获取文件缩略图的链接\n$url = $file-\u003egetThumbUrl();\n```\n\n由已知的 URL 创建文件:\n\n```php\n$file = File::createWithUrl(\"image.png\", \"http://example.net/image.png\");\ntry {\n    $file-\u003esave();\n} catch (CloudException $ex) {\n    // 云存储返回错误，保存失败\n}\n```\n\n更多文档请参考\n[PHP 数据存储开发指南](https://leancloud.cn/docs/leanstorage_guide-php.html)\n\n贡献\n----\n\nSee Hacking.md if you'd like to contribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fphp-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleancloud%2Fphp-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fphp-sdk/lists"}