2 changed files with 142 additions and 0 deletions
@ -0,0 +1,39 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\api\controller; |
||||
|
|
||||
|
use app\common\YingShiSdk\YingShiSdk; |
||||
|
|
||||
|
class YingShiDevice extends Base |
||||
|
{ |
||||
|
|
||||
|
public function deviceStatus() |
||||
|
{ |
||||
|
try { |
||||
|
|
||||
|
$curlData = YingShiSdk::DeviceStatus(); |
||||
|
|
||||
|
if (!is_array($curlData)) throw new \Exception("cURL Error #:" . $curlData); |
||||
|
|
||||
|
return $this->jsonSuccess('操作成功',$curlData); |
||||
|
} catch (\Exception $e) { |
||||
|
return $this->jsonError($e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function deviceInfo() |
||||
|
{ |
||||
|
try { |
||||
|
|
||||
|
$curlData = YingShiSdk::DeviceInfo(); |
||||
|
|
||||
|
if (!is_array($curlData)) throw new \Exception("cURL Error #:" . $curlData); |
||||
|
|
||||
|
return $this->jsonSuccess('操作成功',$curlData); |
||||
|
|
||||
|
} catch (\Exception $e) { |
||||
|
return $this->jsonError($e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,103 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\common\YingShiSdk; |
||||
|
|
||||
|
class YingShiSdk |
||||
|
{ |
||||
|
/** |
||||
|
* accessToken |
||||
|
* @var string |
||||
|
*/ |
||||
|
protected static $accessToken = 'at.2ym6lk525ytwzdj5b401etyx910qjm87-6f0brt6m6g-0pxk61a-qiocb8src'; |
||||
|
|
||||
|
/** |
||||
|
* 设备ID |
||||
|
* @var string |
||||
|
*/ |
||||
|
protected static $deviceSerial = 'BB6305392'; |
||||
|
|
||||
|
/** |
||||
|
* 获取设备状态信息 |
||||
|
* @return mixed|string |
||||
|
*/ |
||||
|
public static function DeviceStatus() |
||||
|
{ |
||||
|
$url = 'https://open.ys7.com/api/lapp/device/status/get'; |
||||
|
|
||||
|
$curlData = self::curlYingshiApi($url,self::$accessToken,self::$deviceSerial); |
||||
|
|
||||
|
$err = $curlData['err']; |
||||
|
$response = $curlData['response']; |
||||
|
|
||||
|
if ($err) return "cURL Error #:" . $err; |
||||
|
|
||||
|
$jsonResponse = json_decode($response,true); |
||||
|
|
||||
|
if ($jsonResponse['code'] != '200') return $jsonResponse['msg']; |
||||
|
|
||||
|
return $jsonResponse['data']; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取单个设备信息 |
||||
|
* @return mixed|string |
||||
|
*/ |
||||
|
public static function DeviceInfo() |
||||
|
{ |
||||
|
|
||||
|
$url = 'https://open.ys7.com/api/lapp/device/info'; |
||||
|
|
||||
|
$curlDataInfo = YingShiSdk::curlYingshiApi($url,self::$accessToken,self::$deviceSerial); |
||||
|
|
||||
|
$err = $curlDataInfo['err']; |
||||
|
|
||||
|
if ($err) return "cURL Error #:" . $err; |
||||
|
|
||||
|
$response = $curlDataInfo['response']; |
||||
|
|
||||
|
$jsonResponse = json_decode($response,true); |
||||
|
|
||||
|
if ($jsonResponse['code'] != '200') return $jsonResponse['msg']; |
||||
|
|
||||
|
return $jsonResponse['data']; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 请求接口数据 |
||||
|
* @param $url // 请求地址 |
||||
|
* @param $accessToken // token |
||||
|
* @param $deviceSerial // 设备ID |
||||
|
* @return array |
||||
|
*/ |
||||
|
public static function curlYingshiApi($url,$accessToken,$deviceSerial) |
||||
|
{ |
||||
|
$curl = curl_init(); |
||||
|
|
||||
|
curl_setopt_array($curl, [ |
||||
|
CURLOPT_URL => "{$url}?accessToken={$accessToken}&deviceSerial={$deviceSerial}", |
||||
|
CURLOPT_RETURNTRANSFER => true, |
||||
|
CURLOPT_ENCODING => "", |
||||
|
CURLOPT_MAXREDIRS => 10, |
||||
|
CURLOPT_TIMEOUT => 30, |
||||
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
|
CURLOPT_CUSTOMREQUEST => "POST", |
||||
|
CURLOPT_HTTPHEADER => [ |
||||
|
"Content-Type: application/x-www-form-urlencoded" |
||||
|
], |
||||
|
]); |
||||
|
|
||||
|
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); |
||||
|
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false); |
||||
|
curl_setopt($curl,CURLOPT_SSLVERSION,0); |
||||
|
|
||||
|
$response = curl_exec($curl); |
||||
|
$err = curl_error($curl); |
||||
|
|
||||
|
curl_close($curl); |
||||
|
|
||||
|
return [ |
||||
|
'err' => $err, |
||||
|
'response' => $response |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue