分享PHP写的机智云demo代码
本帖最后由 Genius 于 2016-7-14 23:57 编辑附件压宿包中的init.php 有说明,可直接运行。有问题联系我QQ 75980367,一起学习。:)
示例:
<?php
class Dev
{
// 获取设备最近上传数据
public static function devdata_latest($did = '')
{
return Gokit_Http::get("app/devdata/{$did}/latest");
}
// 获取设备历史数据
public static function devdata_history($param = [])
{
$uri = 'app/devdata2';
if (isset($param['product_key']) && $param['product_key']) {
$uri.='?product_key='.$param['product_key'];
} else {
return 'product key error';
}
if (isset($param['did']) && $param['did']) {
$uri.='?did='.$param['did'];
}
if (isset($param['start_ts']) && $param['start_ts']) {
$uri.='?start_ts='.$param['start_ts'];
}
if (isset($param['end_ts']) && $param['end_ts']) {
$uri.='?end_ts='.$param['end_ts'];
}
if (isset($param['name']) && $param['name']) {
$uri.='?name='.$param['name'];
}
if (isset($param['limit']) && $param['limit']) {
$uri.='?limit='.$param['limit'];
}
if (isset($param['skip']) && $param['skip']) {
$uri.='?skip='.$param['skip'];
}
// http://api.gizwits.com/app/devdata2?product_key=pk&did=gdGn7PzAYf4VrhnVag5x8D&start_ts=1349032093&end_ts=1349032093&name=temp&limit=20&skip=0
return Gokit_Http::get($uri);
}
// 获取设备信息
public static function info($did = '')
{
if (!$did) {
return 'did is null';
}
return Gokit_Http::get("app/devices/{$did}");
}
// 绑定关系
public static function bindings($limit = 10, $skip = 0)
{
return Gokit_Http::get("app/bindings?show_disabled=1&limit={$limit}&skip={$skip}");
}
// 绑定设备@TODO 绑定多个设备需要进行修改
public static function bind($did = '', $passcode = 'gokit', $remark = '')
{
return Gokit_Http::post("app/bindings", ['devices' => [[
'did' => $did,
'passcode' => $passcode,
'remark' => $remark,
]]]);
}
public static function unbind($did = '')
{
return Gokit_Http::delete("app/bindings", ['devices' => [[
'did' => $did
]]]);
}
public static function select($product_key, $mac)
{
return Gokit_Http::get("app/devices?product_key={$product_key}&mac={$mac}");
}
}
?><?php
class Gokit_Http
{
public static $ch = null;
public static $url_prefix = 'http://api.gizwits.com/';
// public static $url_prefix = 'http://gokit_test.com/';
public static $appid = '';
public static $token = '';
public static $headerArr = [];
// 初始化curl
public static function init()
{
if (!self::$ch) {
self::$ch = curl_init();
}
global $appid,$token;
self::$headerArr[] = 'Content-Type: application/json';
if ($appid) {
self::$headerArr[] = "X-Gizwits-Application-Id: ".$appid;
}
if ($token) {
self::$headerArr[] = "X-Gizwits-User-token: ".$token;
}
}
// post 请求
public static function post($uri, $data)
{
self::init();
// 传输的json数据
$data = json_encode($data);
// curl 设置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
curl_setopt(self::$ch, CURLOPT_POST, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );//构造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
$r = curl_exec(self::$ch);
curl_close(self::$ch);
self::$ch = null;
// 处理结果
// test
return $r;
return self::response($r);
}
// get 请求
public static function get($uri)
{
self::init();
// curl 设置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );//构造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
$r = curl_exec ( self::$ch );
curl_close ( self::$ch );
self::$ch = null;
// 处理结果
return self::response($r);
}
// get 请求
public static function delete($uri, $data = [])
{
self::init();
// curl 设置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
$data = json_encode($data);
curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );//构造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
curl_setopt(self::$ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$r = curl_exec ( self::$ch );
curl_close ( self::$ch );
self::$ch = null;
// 处理结果
return self::response($r);
}
// 处理输出
public static function response($r)
{
if ($r) {
$r = json_decode($r, true);
if ($r) {
return $r;
}
}
return '请求失败:'.var_export($r, true);
}
}
?>
太牛了,谢谢分享。 谢谢分享,一起学习 正好用到,谢谢 666666666 获取设备历史数据 一直没成功,楼主可否指导一下? 谢谢分享!!! 这个php语法array直接换成[],应该是Php7吧,焕然一新的感觉! 好厉害:handshake
页:
[1]