first commit

This commit is contained in:
编码猿
2024-09-27 01:06:04 +08:00
commit cbed24c40d
205 changed files with 8732 additions and 0 deletions

View File

@@ -0,0 +1 @@
2361b437-6e8d-4b57-b485-f1795f4120d6

View File

@@ -0,0 +1,93 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobApp extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 获取app的信息, 指定appId则获取某个app的信息
* @param $email 登录的用户名
* @param $password 登录的密码
* @param $appId
*/
public function getApp($email, $password, $appId = 0)
{
if (!empty($email) && !empty($password)) {
if ($appId != 0) {
$requestUrl = 'apps/' . $appId;
} else {
$requestUrl = 'apps';
}
$sendRequest = $this->sendRequest(array(
'X-Bmob-Email' => $email,
'X-Bmob-Password' => $password,
'method' => 'GET',
'sendRequestUrl' => $requestUrl,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 创建app
* @param $email 登录的用户名
* @param $password 登录的密码
* @param $data
*/
public function createApp($email, $password, $data)
{
if (!empty($email) && !empty($password) && !empty($data)) {
$sendRequest = $this->sendRequest(array(
'X-Bmob-Email' => $email,
'X-Bmob-Password' => $password,
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => "apps",
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 修改app的信息
* @param $email 登录的用户名
* @param $password 登录的密码
* @param $data
*/
public function updateApp($email, $password, $id, $data)
{
if (!empty($email) && !empty($password) && !empty($id) && !empty($data)) {
$sendRequest = $this->sendRequest(array(
'X-Bmob-Email' => $email,
'X-Bmob-Password' => $password,
'method' => 'PUT',
'data' => $data,
'sendRequestUrl' => "apps/" . $id,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,43 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobBatch extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 批量操作
* @param array $data 批量操作的数据
*/
public function batch($data)
{
if (!empty($data)) {
//重设对象的属性
$this->cleanData();
$this->data["requests"] = $data;
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'batch',
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('批量操作时请指定操作的数据');
}
}
}
?>

View File

@@ -0,0 +1,42 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobBql extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 批量操作
* @param array $data 查询条件
*/
public function query($data)
{
if (!empty($data)) {
$this->data["requests"] = $data;
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'cloudQuery',
'urlParams' => $data,
));
return $sendRequest;
} else {
$this->throwError('查询条件不能为空');
}
}
}
?>

View File

@@ -0,0 +1,46 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobObject object对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobCloudCode extends BmobRestClient
{
private $_cloudCodeName = '';
public function __construct($codeName = '')
{
if ($codeName != '') {
$this->_cloudCodeName = $codeName;
} else {
$this->throwError('创建对象时请包含对象id');
}
parent::__construct();
}
/**
* 获取对象
* @param array $condition ,查询条件
*/
public function get($data = array())
{
if ($this->_cloudCodeName != '') {
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'functions/' . $this->_cloudCodeName,
'data' => $data,
));
return $sendRequest;
} else {
$this->throwError('获取对象时请指定对象id');
}
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* BmobException 异常类
* @author jeff
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobException extends Exception
{
public function __construct($message="", $code = 0) {
//codes are only set by a bmob.com error
if($code != 0){
$message = "bmob.cn error: ".$message;
}
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}

View File

@@ -0,0 +1,101 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobFile extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 删除文件
* @param array $data 删除文件的数据
*/
public function delete($url)
{
if (!empty($url) ) {
//重设对象的属性
$this->cleanData();
$sendRequest = $this->sendRequest(array(
'method' => 'DELETE',
'sendRequestUrl' => 'files/' . $url,
));
return $sendRequest;
} else {
$this->throwError('请输入url');
}
}
/**
* 用CDN上传文件
如果需要上传大文件,请在代码中调整php的配置:
ini_set('max_execution_time', 30); //每个PHP页面运行的最大时间值(秒)默认30秒
ini_set('max_input_time', 60); //每个PHP页面接收数据所需的最大时间默认60秒
ini_set('memory_limit', '8m'); //每个PHP页面所吃掉的最大内存默认8M
ini_set('upload_max_filesize', '2m'); //即允许上传文件大小的最大值。默认为2M
ini_set('post_max_size', '8m'); //指通过表单POST给PHP的所能接收的最大值包括表单里的所有值。默认为8M
* @param array $data 批量操作的数据
*/
public function uploadFile2($fileName, $filePath)
{
if (!empty($fileName) && !empty($filePath)) {
//重设对象的属性
$this->cleanData();
$this->data = file_get_contents($filePath);
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'files/' . $fileName,
'data' => $this->data,
), 2);
return $sendRequest;
} else {
$this->throwError('请输入文件名和文件路径');
}
}
/**
* 用uploadFile2接口上传的文件只能用这个接口删除文件
* @param array $url
*/
public function delete2($cdn, $url)
{
if (!empty($url) && !empty($cdn) ) {
//重设对象的属性
$this->cleanData();
// $path = pathinfo($url);
// if( !$path ){
// $this->throwError('解析url错误, 正确的url是类似于:http://bmob-cdn-10.b0.upaiyun.com/2017/02/08/f39178e3409990ca80e0d9d60ecef768.png');
// }
if(strpos($url, "http://")>=0) {
$url = trim($url, "http://");
} else {
$url = trim($url, "https://");
}
$url = substr($url, strpos($url, "/") + 1);
$sendRequest = $this->sendRequest(array(
'method' => 'DELETE',
'sendRequestUrl' => 'files/' .$cdn."/". $url,
), 2 );
return $sendRequest;
} else {
$this->throwError('请输入url');
}
}
}
?>

View File

@@ -0,0 +1,65 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobImage extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 生成缩微图
* @param array $data 参数
*/
public function imageThumbnail($data)
{
if (!empty($data)) {
//重设对象的属性
$this->cleanData();
$this->data = $data;
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'images/thumbnail',
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 生成水印
* @param array $data 参数
*/
public function imagesWatermark($data)
{
if (!empty($data)) {
$this->data = $data;
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'images/watermark',
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,429 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobObject object对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobObject extends BmobRestClient
{
public $_includes = array();
private $_className = '';
public function __construct($class = '', $sessionToken = '')
{
if ($class != '') {
$this->_className = $class;
} else {
$this->throwError('创建对象时请包含对象id');
}
if ($sessionToken != '') {
$this->bmobSessionToken = $sessionToken;
}
parent::__construct();
}
public function getRequestUrl($id=""){
$className = $this->_className;
if( "_User" == $className ){
$className = "users";
} elseif ( "_Installation" == $className ) {
$className = "installations";
}
if( $id ){
if ( $this->_className!="_User" && $this->_className!="_Installation") {
return 'classes/' . $className . '/' . $id;
}else{
return $className . '/' . $id;
}
} else {
if ( $this->_className!="_User" && $this->_className!="_Installation") {
return 'classes/' . $className ;
}else{
return $className ;
}
}
}
/**
* 添加对象
* @param array $data 对象的属性数组
*
*/
public function create($data = array())
{
//重设对象的属性
$this->setData($data);
if (count($this->data) > 0 && $this->_className != '') {
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => $this->getRequestUrl(""),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('创建对象时请添加对象数据或指定对象id');
}
}
/**
* 获取对象
* @param string $id 对象id, 当为空时表示获取所有对象
* @param array $condition ,查询条件
*/
public function get($id = "", $condition = array())
{
if ($this->_className != '') {
if ($id) {
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => $this->getRequestUrl($id),
'condition' => $condition,
));
} else {
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => $this->getRequestUrl(""),
'condition' => $condition,
));
}
return $sendRequest;
} else {
$this->throwError('获取对象时请指定对象id');
}
}
/**
* 更新对象的属性
* @param string $id 对象id
* @param array $data 对象的属性数组
*/
public function update($id, $data = array())
{
if ($this->_className != '' || !empty($id)) {
if ($data) {
//添加对象的属性
$this->setData($data);
} else {
$this->throwError('请指定要更新的属性');
}
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改对象时请指定对象id');
}
}
/**
* 删除对象
* @param string $id 对象id
*/
public function delete($id)
{
if ($this->_className != '' || !empty($id)) {
$sendRequest = $this->sendRequest(array(
'method' => 'DELETE',
'sendRequestUrl' => $this->getRequestUrl($id),
));
return $sendRequest;
} else {
$this->throwError('删除对象时请指定对象id');
}
}
/**
* 任何数字字段进行原子增加或减少的功能
* @param string $id 对象id
* @param string $field 需要修改的数字字段
* @param int $amount 不加负号表示增加, 加负号表示减少
*/
public function increment($id, $field, $amount)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' || !empty($id)) {
$this->data[$field] = $this->dataType('increment', $amount);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改对象时请指定对象id');
}
}
/**
* 在一个对象中删除一个字段
* @param string $id 对象id
* @param string $field 需要删除的字段
*/
public function deleteField($id, $field)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($id) && !empty($field)) {
$this->data[$field] = $this->dataType('deleteField', $field);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改对象时请指定对象id');
}
}
/**
* 添加数组数据
* @param string $field 需要修改的字段
* @param string $data 添加的数组
*/
public function addArray($field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('addArray', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => $this->getRequestUrl(""),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('添加对象时请指定对象field或数据');
}
}
/**
* 修改数组数据
* @param string $id id
* @param string $field 需要修改的字段
* @param string $data 修改的数组
*/
public function updateArray($id, $field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($id) && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('addArray', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改对象时请指定对象id和field和数据');
}
}
/**
* 删除数组数据
* @param string $id id
* @param string $field 需要修改的字段
* @param string $data 删除的数组
*/
public function deleteArray($id, $field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($id) && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('delArray', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('删除时请指定对象id和field和数据');
}
}
/**
* 添加关联对象(1对1)
* @param string $field 需要添加关联的字段
* @param string $otherObject 需要修改的字段
* @param string $otherObjectId 删除的数组
*/
public function addRelPointer($data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($data) ) {
$this->data = $this->dataType('addRelPointer', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => $this->getRequestUrl(""),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('添加关联对象时请指定对象id和field和数据');
}
}
/**
* 添加关联对象(1对多)
* @param string $field 需要添加关联的字段
* @param array $data 关联的数据
*/
public function addRelRelation($field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('addRelRelation', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => $this->getRequestUrl(""),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('添加关联对象时请指定对象id和data');
}
}
/**
* 修改关联对象(1对1)
* @param string $id id
* @param string $field 需要添加关联的字段
* @param string $otherObject 需要修改的字段
* @param string $otherObjectId 删除的数组
*/
public function updateRelPointer($id, $field, $otherObject, $otherObjectId)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($field) && !empty($otherObject) && !empty($otherObjectId)) {
$this->data[$field] = $this->dataType('updateRelPointer', array($field, $otherObject, $otherObjectId));
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改关联对象时请指定对象id和field和数据');
}
}
/**
* 修改关联对象(1对多)
* @param string $id
* @param string $field 需要添加关联的字段
* @param array $data 关联的数据
*/
public function updateRelRelation($id, $field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($id) && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('addRelRelation', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('修改关联对象时请指定对象id和field和data');
}
}
/**
* 删除关联对象
* @param string $id
* @param string $field 需要添加关联的字段
* @param array $data 关联的数据
*/
public function deleteRelation($id, $field, $data)
{
//重设对象的属性
$this->cleanData();
if ($this->_className != '' && !empty($id) && !empty($field) && !empty($data)) {
$this->data[$field] = $this->dataType('removeRelation', $data);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => $this->getRequestUrl($id),
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('删除关联对象时请指定对象id和field和data');
}
}
}
?>

View File

@@ -0,0 +1,61 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobPay 支付类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobPay extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 查询订单
* @param $id
*/
public function getOrder($id)
{
if (!empty($id)) {
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'pay/' . $id,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 支付扫码接口
* @param $orderPrice 价格
* @param $productName 商品名称
* @param $body 商品描述
* @param $payment 2:微信 3.qq
*/
public function webPay($orderPrice, $productName, $body, $payment)
{
if (!empty($orderPrice) && !empty($productName) && !empty($body) ) {
$this->cleanData();
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'pay' ,
'data' => array("order_price"=>$orderPrice, "product_name"=>$productName, "body"=>$body, "payment"=>$payment),
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,80 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobPush extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 保存设备信息
* @param $data data
*/
public function addInstallations($data)
{
if (!empty($data)) {
//重设对象的属性
$this->cleanData();
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => 'installations',
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 更新设备信息
* @param $data data
*/
public function updateInstallations($id, $data)
{
if (!empty($data)) {
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'data' => $data,
'sendRequestUrl' => 'installations/' . $id,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 推送
* @param $data data
*/
public function push($data)
{
if (!empty($data)) {
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => 'push',
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,371 @@
<?php
include_once 'BmobConfig.class.php';
include_once 'BmobException.class.php';
/**
* BmobRestClient 主类所有的api请求都需要使用这个类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobRestClient
{
private $_bmobAppid = '';
private $_bmobRestkey = '';
private $_bmobUrl = '';
private $_bmobUrlTwo = '';
public $data;
public $sendRequestUrl = '';
public $responseData = '';
public $masterKey = '';
public $bmobSessionToken = '';
protected function __construct()
{
$this->_bmobAppid = BmobConfig::APPID;
$this->_bmobRestkey = BmobConfig::RESTKEY;
$this->_bmobUrl = BmobConfig::BMOBURL;
$this->_bmobUrlTwo = BmobConfig::BMOBURLTWO;
if (empty($this->_bmobAppid) || empty($this->_bmobRestkey)) {
$this->throwError('必须要设置Application ID 和 REST API Key');
}
if (!$this->_bmobUrlTwo) {
$this->throwError('必须要在BmobConfig.class.php中设置BMOBURLTWO的值, 请参考github上的设置');
}
$version = curl_version();
if (!$version['features'] & CURL_VERSION_SSL) {
$this->throwError('不支持ssl的链接');
}
}
/**
* 重设对象的属性
* @param array $data
*/
public function setData($data = array())
{
//每次使用前先清空对象属性数组
$this->data = array();
if ($data) {
foreach ($data as $name => $value) {
if ($name != '_className') {
$this->data[$name] = $value;
}
}
}
}
/**
* 设置master key
* @param string $masterKey
*/
public function setMasterkey($masterKey){
$this->masterKey = $masterKey;
}
/**
* 重设对象的属性
* @param array $data
*/
public function cleanData()
{
//每次使用前先清空对象属性数组
$this->data = array();
}
/**
* 所有的请求都通过这个方法发送
* @param $args
*/
protected function sendRequest($args, $apiVersion = 1)
{
$c = curl_init();
curl_setopt($c, CURLOPT_TIMEOUT, 30);
curl_setopt($c, CURLOPT_USERAGENT, 'bmob-php-library/1.0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLINFO_HEADER_OUT, true);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
//支持SSL
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
if( 2 == $apiVersion ){
$url = $this->_bmobUrlTwo . $args['sendRequestUrl'];
} else {
$url = $this->_bmobUrl . $args['sendRequestUrl'];
}
//Users的方法
$header = array(
'Content-Type: application/json',
'X-Bmob-SDK-Type: php',
'X-bmob-Application-Id: ' . $this->_bmobAppid,
'X-bmob-REST-API-Key: ' . $this->_bmobRestkey,
);
if (substr($args['sendRequestUrl'], 0, 5) == 'users') {
if (isset($args['sessionToken'])) {
//需要传入token, 用于更新删除的操作
array_push($header, 'X-Bmob-Session-Token:'.$args['sessionToken']);
} elseif (isset($args['masterKey'])) {
//需要传入X-Bmob-Master-Key
array_push($header, 'X-Bmob-Master-Key:'.$args['masterKey']);
} else {
}
} elseif (substr($args['sendRequestUrl'], 0, 7) == 'classes') {
} elseif (strpos($args['sendRequestUrl'], "updateUserPassword") !== false) {
//对象的方法
array_push($header, 'X-Bmob-Session-Token:'.$args['sessionToken']);
} elseif (strpos($args['sendRequestUrl'], "roles") !== false && $args['method'] == "DELETE") {
//对象的方法
array_push($header, 'X-Bmob-Session-Token:'.$args['sessionToken']);
} elseif (strpos($args['sendRequestUrl'], "apps") !== false) {
//对象的方法
} elseif (strpos($args['sendRequestUrl'], "schemas") !== false) {
//对象的方法
} else {
}
if( $this->bmobSessionToken ){
array_push($header, 'X-Bmob-Session-Token:'.$this->bmobSessionToken);
}
if( $this->masterKey ){
array_push($header, 'X-Bmob-Master-Key:'.$this->masterKey);
}
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
// var_dump($url);
// print_r($header);
//生成post data
if ($args['method'] == 'PUT' || $args['method'] == 'POST') {
$postData = $args['data'];
if (strpos($args['sendRequestUrl'], "files") === false || strpos($args['sendRequestUrl'], "files")>0) { //非上传文件的操作把body内容变为json
$postData = json_encode($args['data']);
}
// echo "postdata:".$postData;
curl_setopt($c, CURLOPT_POSTFIELDS, $postData);
}
//生成查询的条件
if ($args['method'] == 'GET' && isset($args['condition']) && $args['condition']) {
foreach ($args['condition'] as $con) {
list($key, $value) = explode("=", $con);
$args['urlParams'][$key] = $value;
}
}
if ($args['sendRequestUrl'] == 'login') {
$urlParams = http_build_query($args['data'], '', '&');
$url = $url . '?' . $urlParams;
}
if (array_key_exists('urlParams', $args)) {
if( $args['sendRequestUrl'] == "cloudQuery" ) {
$url = $url . '?bql=' .urlencode($args['urlParams']['bql']);
if( isset($args['urlParams']['values']) ) {
$url = $url . '&values=' .urlencode($args['urlParams']['values']);
}
}else {
$urlParams = http_build_query($args['urlParams'], '', '&');
$url = $url . '?' . $urlParams;
}
}
curl_setopt($c, CURLOPT_URL, $url);
$response = curl_exec($c);
// echo "response_code:".$response;
if (!$response) {
trigger_error(curl_error($c));
}
$responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
$expectedCode = array('200', '201');
return $this->checkResponse($args,$response, $responseCode, $expectedCode);
}
/**
* 生成特殊的数据类型
* @param string $type
* @param array $params
*/
protected function dataType($type, $params)
{
if ($type != '') {
switch ($type) {
case 'date':
$return = array(
"__type" => "Date",
"iso" => date("c", strtotime($params))
);
break;
case 'geopoint':
$return = array(
"__type" => "GeoPoint",
"latitude" => floatval($params[0]),
"longitude" => floatval($params[1])
);
break;
case 'increment':
$return = array(
"__op" => "Increment",
"amount" => $params[0]
);
break;
case 'deleteField':
$return = array(
"__op" => "Delete",
);
break;
case 'addArray':
$return = array(
"__op" => "Add",
"objects" => $params,
);
break;
case 'updateArray':
$return = array(
"__op" => "AddUnique",
"objects" => $params,
);
break;
case 'delArray':
$return = array(
"__op" => "Remove",
"objects" => $params,
);
break;
case 'addRelPointer':
$data = array();
foreach ($params as $param) {
$key = $param[0];
$data[$key] = array(
"__type" => "Pointer",
"className" => $param[1],
"objectId" => $param[2],
);
}
return $data;
break;
case 'updateRelPointer':
return array(
"__type" => "Pointer",
"className" => $params[1],
"objectId" => $params[2],
);
break;
case 'addRelRelation':
$data = array();
foreach ($params as $param) {
$data[] = array(
"__type" => "Pointer",
"className" => $param[0],
"objectId" => $param[1],
);
}
$return = array(
"__op" => "AddRelation",
"objects" => $data,
);
break;
case 'removeRelation':
$data = array();
foreach ($params as $param) {
$data[] = array(
"__type" => "Pointer",
"className" => $param[0],
"objectId" => $param[1],
);
}
$return = array(
"__op" => "RemoveRelation",
"objects" => $data,
);
break;
default:
$return = false;
break;
}
return $return;
}
}
/**
* 抛出异常
* @param $msg
* @param $code
*/
protected function throwError($msg, $code = 0)
{
throw new BmobException($msg, $code);
}
/**
* 检查返回值
* @param string $response
* @param array $responseCode
* @param array $expectedCode
*/
private function checkResponse($args, $response, $responseCode, $expectedCode)
{
if (!in_array($responseCode, $expectedCode)) {
$error = json_decode($response, true);
$msg = isset($error['error']) ? $error['error'] : "";
$code = isset($error['code']) ? $error['code'] : 0;
$this->throwError($msg, $code);
} else {
//check for empty return
if ($response == '{}') {
return true;
} else {
$decodeResponse = json_decode($response);
//把云端代码返回值从result中剥离出来
if (isset($decodeResponse->result)) {
$tradeNo = "";
//订单接口,要把订单号返回
if("POST" == $args["method"] && "pay" == $args["sendRequestUrl"]) {
$tradeNo = $decodeResponse->out_trade_no;
}
$decodeResponse = $decodeResponse->result;
if("POST" == $args["method"] && "pay" == $args["sendRequestUrl"]) {
$decodeResponse->tradeNo = $tradeNo;
}
}
return $decodeResponse;
}
}
}
}
?>

View File

@@ -0,0 +1,102 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobRole extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 创建角色
* @param array $data 参数
*/
public function createRole($data)
{
if (!empty($data)) {
//重设对象的属性
$this->cleanData();
$this->data = $data;
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'roles',
'data' => $this->data,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 获取角色
* @param $id id
*/
public function getRole($id)
{
if (!empty($id)) {
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'roles/' . $id,
));
return $sendRequest;
} else {
$this->throwError('id不能为空');
}
}
/**
* 更新角色
* @param $id id
*/
public function updateRole($id, $field, $op, $data)
{
if (!empty($id) && !empty($field) && !empty($op) && !empty($data)) {
$this->data[$field] = array("__op" => $op, "objects" => $data);
$sendRequest = $this->sendRequest(array(
'method' => 'PUT',
'data' => $this->data,
'sendRequestUrl' => 'roles/' . $id,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 删除角色
* @param $id id
* @param $sessionToken sessionToken
*/
public function deleteRole($id, $sessionToken)
{
if (!empty($id) && !empty($sessionToken)) {
$sendRequest = $this->sendRequest(array(
'method' => 'DELETE',
'data' => $this->data,
'sessionToken' => $sessionToken,
'sendRequestUrl' => 'roles/' . $id,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,114 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobSchemas extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 获取Schemas的信息
* @param $masterKey Master-Key
* @param $schemaName schemaName的名称
* @param $data
*/
public function getSchemas($masterKey, $schemaName="")
{
if (!empty($masterKey)) {
$url="";
if( $schemaName ) {
$url="schemas/" . $schemaName;
} else {
$url= "schemas" ;
}
$sendRequest = $this->sendRequest(array(
'X-Bmob-Master-Key' => $masterKey,
'method' => 'GET',
'sendRequestUrl' => $url,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* create Schemas
* @param $masterKey Master-Key
* @param $schemaName schemaName的名称
* @param $data
*/
public function createSchemas($masterKey, $schemaName, $data)
{
if (!empty($masterKey) && !empty($schemaName) && !empty($data)) {
$sendRequest = $this->sendRequest(array(
'X-Bmob-Master-Key' => $masterKey,
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => "schemas/" . $schemaName,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* update Schemas
* @param $masterKey Master-Key
* @param $schemaName schemaName的名称
* @param $data
*/
public function updateSchemas($masterKey, $schemaName, $data)
{
if (!empty($masterKey) && !empty($schemaName) && !empty($data)) {
$sendRequest = $this->sendRequest(array(
'X-Bmob-Master-Key' => $masterKey,
'method' => 'PUT',
'data' => $data,
'sendRequestUrl' => "schemas/" . $schemaName,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* delete Schemas
* @param $masterKey Master-Key
* @param $schemaName schemaName的名称
*/
public function deleteSchemas($masterKey, $schemaName)
{
if (!empty($masterKey) && !empty($schemaName)) {
$sendRequest = $this->sendRequest(array(
'X-Bmob-Master-Key' => $masterKey,
'method' => 'DELETE',
'sendRequestUrl' => "schemas/" . $schemaName,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,67 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobSms extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 发送短信验证码
* @param $mobile 发送的手机号
*/
public function sendSmsVerifyCode($mobile, $template="")
{
if (!empty($mobile)) {
if( $template ){
$data = array("mobilePhoneNumber" => $mobile, "template"=>$template);
} else {
$data = array("mobilePhoneNumber" => $mobile);
}
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => 'requestSmsCode',
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 验证短信验证码
* @param $mobile 发送的手机号
* @param $verifyCode 短信验证码
*/
public function verifySmsCode($mobile, $verifyCode)
{
if (!empty($mobile) && !empty($verifyCode)) {
$data = array("mobilePhoneNumber" => $mobile);
$sendRequest = $this->sendRequest(array(
'method' => 'POST',
'data' => $data,
'sendRequestUrl' => 'verifySmsCode/' . $verifyCode,
));
return $sendRequest;
} else {
$this->throwError('参数不能为空');
}
}
}
?>

View File

@@ -0,0 +1,35 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser batch对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobTimestamp extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 获取服务器时间
*/
public function getTimestamp()
{
$sendRequest = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => "timestamp",
));
return $sendRequest;
}
}
?>

View File

@@ -0,0 +1,292 @@
<?php
include_once 'BmobRestClient.class.php';
/**
* BmobUser User对象类
* @author karvinchen
* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
*/
class BmobUser extends BmobRestClient
{
public function __construct($class = '')
{
parent::__construct();
}
/**
* 用户注册
* @param array $data 需要传入的参数
*/
public function register($data = array())
{
$this->cleanData();
if (!empty($data)) {
$request = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'users',
'data' => $data
));
return $request;
} else {
$this->throwError('输入参数不能为空');
}
}
/**
* 用户登录, 其中username和password为必填字段
* @param string $username 用户名, 必填
* @param string $password 密码, 必填
*/
public function login($username, $password)
{
if (!empty($username) || !empty($password)) {
$request = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'login',
'data' => array(
'username' => $username,
'password' => $password
)
));
return $request;
} else {
$this->throwError('用户名和密码不能为空');
}
}
/**
* 用手机号登录, 其中mobilePhoneNumber和smsCode为必填字段
* @param string $mobilePhoneNumber 手机号
* @param string $smsCode 验证码
*/
public function loginByMobile($mobilePhoneNumber, $smsCode)
{
if (!empty($mobilePhoneNumber) || !empty($smsCode)) {
$request = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'users',
'data' => array(
'mobilePhoneNumber' => $mobilePhoneNumber,
'smsCode' => $smsCode
)
));
return $request;
} else {
$this->throwError('手机号和验证码不能为空');
}
}
/**
* 获取用户的信息
* @param string $userId 用户id, 必填
*/
public function get($userId = 0, $condition = array())
{
if ($userId) {
//获取某个用户的信息
$request = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'users/' . $userId,
));
return $request;
} else {
//获取所有用户的信息
$request = $this->sendRequest(array(
'method' => 'GET',
'sendRequestUrl' => 'users',
'condition' => $condition,
));
return $request;
}
}
/**
* 更新用户的信息
* @param string $userId 用户id, 必填
* @param string $sessionToken 在用户登录或注册后获取, 必填
* @param array $data 需要更新的用户属性
*/
public function update($userId, $sessionToken, $data = array())
{
if (!empty($userId) || !empty($sessionToken)) {
if ($data) {
$request = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => 'users/' . $userId,
'sessionToken' => $sessionToken,
'data' => $data
));
} else {
$this->throwError('请输入需要更新的属性');
}
return $request;
} else {
$this->throwError('用户id和sessionToken不能为空');
}
}
/**
* 通过MasterKey更新用户的信息
* @param string $userId 用户id, 必填
* @param string $masterKey 后台应用密钥中的Master Key
* @param array $data 需要更新的用户属性
*/
public function updateByMasterKey($userId, $masterKey, $data = array())
{
if (!empty($userId) || !empty($masterKey)) {
if ($data) {
$request = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => 'users/' . $userId,
'masterKey' => $masterKey,
'data' => $data
));
} else {
$this->throwError('请输入需要更新的属性');
}
return $request;
} else {
$this->throwError('用户id和masterKey不能为空');
}
}
/**
* 请求重设密码,前提是用户将email与他们的账户关联起来
* @param string $email
*/
public function requestPasswordReset($email)
{
if (!empty($email)) {
$request = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'requestPasswordReset',
'data' => array("email" => $email)
));
return $request;
} else {
$this->throwError('email is required for the requestPasswordReset method');
}
}
/**
* 删除用户
* @param string $userId
* @param string $sessionToken
*/
public function delete($userId, $sessionToken)
{
if (!empty($userId) || !empty($sessionToken)) {
$request = $this->sendRequest(array(
'method' => 'DELETE',
'sendRequestUrl' => 'users/' . $userId,
'sessionToken' => $sessionToken
));
return $request;
} else {
$this->throwError('用户id和sessionToken不能为空');
}
}
/**
* 删除用户
* @param string $userId
* @param string $masterKey
*/
public function deleteByMasterKey($userId, $masterKey)
{
if (!empty($userId) || !empty($masterKey)) {
$request = $this->sendRequest(array(
'method' => 'DELETE',
'sendRequestUrl' => 'users/' . $userId,
'masterKey' => $masterKey
));
return $request;
} else {
$this->throwError('用户id和masterKey不能为空');
}
}
/**
* 使用短信验证码进行密码重置
* @param string $oldPassword 旧密码
* @param string $newPassword 新密码
*/
public function resetPasswordBySmsCode($smsCode, $newPassword)
{
if (!empty($newPassword) || !empty($smsCode)) {
$request = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => 'resetPasswordBySmsCode/' . $smsCode,
'data' => array("password" => $newPassword)
));
return $request;
} else {
$this->throwError('密码和短信验证码不能为空');
}
}
/**
* 用户输入一次旧密码做一次校验,旧密码正确才可以修改为新密码
* @param string $userId id
* @param string $sessionToken sessionToken
* @param string $oldPassword 旧密码
* @param string $newPassword 新密码
*/
public function updateUserPassword($userId, $sessionToken, $oldPassword, $newPassword)
{
if (!empty($oldPassword) || !empty($newPassword) || !empty($userId) || !empty($sessionToken)) {
$request = $this->sendRequest(array(
'method' => 'PUT',
'sendRequestUrl' => 'updateUserPassword/' . $userId,
'sessionToken' => $sessionToken,
'data' => array("oldPassword" => $oldPassword, "newPassword" => $newPassword)
));
return $request;
} else {
$this->throwError('参数不能为空');
}
}
/**
* 请求验证Email
* @param string $email email
*/
public function requestEmailVerify($email)
{
if (!empty($email)) {
$request = $this->sendRequest(array(
'method' => 'POST',
'sendRequestUrl' => 'requestEmailVerify',
'data' => array("email" => $email)
));
return $request;
} else {
$this->throwError('email不能为空');
}
}
}
?>