收藏官网首页
查看: 10741|回复: 9

分享PHP写的机智云demo代码

5

主题

13

帖子

271

积分

中级会员

Rank: 3Rank: 3

积分
271
跳转到指定楼层
楼主
发表于 2015-6-24 08:55:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
教您5分钟接入机智云,实现傻瓜式开发
本帖最后由 Genius 于 2016-7-14 23:57 编辑


附件压宿包中的init.php 有说明,可直接运行。有问题联系我QQ 75980367,一起学习。
示例:
  1. <?php
  2. class Dev
  3. {
  4.     // 获取设备最近上传数据
  5.     public static function devdata_latest($did = '')
  6.     {
  7.         return gokit_Http::get("app/devdata/{$did}/latest");
  8.     }

  9.     // 获取设备历史数据
  10.     public static function devdata_history($param = [])
  11.     {
  12.         $uri = 'app/devdata2';
  13.         if (isset($param['product_key']) && $param['product_key']) {
  14.             $uri.='?product_key='.$param['product_key'];
  15.         } else {
  16.             return 'product key error';
  17.         }
  18.         if (isset($param['did']) && $param['did']) {
  19.             $uri.='?did='.$param['did'];
  20.         }
  21.         if (isset($param['start_ts']) && $param['start_ts']) {
  22.             $uri.='?start_ts='.$param['start_ts'];
  23.         }
  24.         if (isset($param['end_ts']) && $param['end_ts']) {
  25.             $uri.='?end_ts='.$param['end_ts'];
  26.         }
  27.         if (isset($param['name']) && $param['name']) {
  28.             $uri.='?name='.$param['name'];
  29.         }
  30.         if (isset($param['limit']) && $param['limit']) {
  31.             $uri.='?limit='.$param['limit'];
  32.         }
  33.         if (isset($param['skip']) && $param['skip']) {
  34.             $uri.='?skip='.$param['skip'];
  35.         }
  36.         // http://api.gizwits.com/app/devdata2?product_key=pk&did=gdGn7PzAYf4VrhnVag5x8D&start_ts=1349032093&end_ts=1349032093&name=temp&limit=20&skip=0
  37.         return Gokit_Http::get($uri);
  38.     }

  39.     // 获取设备信息
  40.     public static function info($did = '')
  41.     {
  42.         if (!$did) {
  43.             return 'did is null';
  44.         }
  45.         return Gokit_Http::get("app/devices/{$did}");
  46.     }

  47.     // 绑定关系
  48.     public static function bindings($limit = 10, $skip = 0)
  49.     {
  50.         return Gokit_Http::get("app/bindings?show_disabled=1&limit={$limit}&skip={$skip}");
  51.     }

  52.     // 绑定设备@TODO 绑定多个设备需要进行修改
  53.     public static function bind($did = '', $passcode = 'gokit', $remark = '')
  54.     {
  55.         return Gokit_Http::post("app/bindings", ['devices' => [[
  56.                    'did'      => $did,
  57.                    'passcode' => $passcode,
  58.                    'remark'   => $remark,
  59.                ]]]);
  60.     }

  61.     public static function unbind($did = '')
  62.     {
  63.         return Gokit_Http::delete("app/bindings", ['devices' => [[
  64.             'did'      => $did
  65.         ]]]);
  66.     }

  67.     public static function select($product_key, $mac)
  68.     {
  69.         return Gokit_Http::get("app/devices?product_key={$product_key}&mac={$mac}");
  70.     }
  71. }
  72. ?>
复制代码
  1. <?php
  2.         class Gokit_Http
  3.         {
  4.                 public static $ch = null;
  5.                 public static $url_prefix = 'http://api.gizwits.com/';
  6.                 // public static $url_prefix = 'http://gokit_test.com/';

  7.                 public static $appid = '';
  8.                 public static $token = '';

  9.                 public static $headerArr = [];

  10.                 // 初始化curl
  11.                 public static function init()
  12.                 {
  13.                         if (!self::$ch) {
  14.                                 self::$ch = curl_init();
  15.                         }
  16.                         global $appid,$token;
  17.                         self::$headerArr[] = 'Content-Type: application/json';
  18.                         if ($appid) {
  19.                                 self::$headerArr[] = "X-Gizwits-Application-Id: ".$appid;
  20.                         }
  21.                         if ($token) {
  22.                                 self::$headerArr[] = "X-Gizwits-User-token: ".$token;
  23.                         }
  24.                 }

  25.                 // post 请求
  26.                 public static function post($uri, $data)
  27.                 {
  28.                         self::init();
  29.                         // 传输的json数据
  30.                         $data = json_encode($data);
  31.                         // curl 设置
  32.                         curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
  33.                         curl_setopt(self::$ch, CURLOPT_POST, 1 );
  34.                         curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
  35.                         curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );  //构造IP
  36.                         curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
  37.                         curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
  38.                         curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
  39.                         $r = curl_exec(self::$ch);
  40.                         curl_close(self::$ch);
  41.                         self::$ch = null;
  42.                         // 处理结果
  43.                         // test
  44.                         return $r;
  45.                         return self::response($r);
  46.                 }

  47.                 // get 请求
  48.                 public static function get($uri)
  49.                 {
  50.                         self::init();
  51.                         // curl 设置
  52.                         curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
  53.                         curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );  //构造IP
  54.                         curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
  55.                         curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
  56.                         curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
  57.                         $r = curl_exec ( self::$ch );
  58.                         curl_close ( self::$ch );
  59.                         self::$ch = null;
  60.                         // 处理结果
  61.                         return self::response($r);
  62.                 }

  63.                 // get 请求
  64.                 public static function delete($uri, $data = [])
  65.                 {
  66.                         self::init();
  67.                         // curl 设置
  68.                         curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
  69.                         $data = json_encode($data);
  70.                         curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
  71.                         curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );  //构造IP
  72.                         curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
  73.                         curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
  74.                         curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
  75.                         curl_setopt(self::$ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  76.                         $r = curl_exec ( self::$ch );
  77.                         curl_close ( self::$ch );
  78.                         self::$ch = null;
  79.                         // 处理结果
  80.                         return self::response($r);
  81.                 }

  82.                 // 处理输出
  83.                 public static function response($r)
  84.                 {
  85.                         if ($r) {
  86.                                 $r = json_decode($r, true);
  87.                                 if ($r) {
  88.                                         return $r;
  89.                                 }
  90.                         }
  91.                         return '请求失败:'.var_export($r, true);
  92.                 }
  93.         }
  94. ?>
复制代码



gokit_test.com.rar

4.2 KB, 下载次数: 182, 下载积分: 威望 1

机智云PHP调用代码分享

9

主题

66

帖子

235

积分

中级会员

Rank: 3Rank: 3

积分
235
沙发
发表于 2015-6-25 06:11:39 | 只看该作者
太牛了,谢谢分享。

19

主题

42

帖子

244

积分

中级会员

Rank: 3Rank: 3

积分
244
板凳
发表于 2015-7-31 17:45:07 | 只看该作者
谢谢分享,一起学习

2

主题

10

帖子

82

积分

注册会员

Rank: 2

积分
82
地板
发表于 2016-5-30 01:23:35 | 只看该作者
正好用到,谢谢

0

主题

5

帖子

40

积分

新手上路

Rank: 1

积分
40
6#
发表于 2016-7-21 16:35:15 | 只看该作者
获取设备历史数据 一直没成功,楼主可否指导一下?

134

主题

404

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11792
8#
发表于 2016-9-30 11:55:29 | 只看该作者
这个php语法array直接换成[],应该是Php7吧,焕然一新的感觉!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表