現在有個項目是甲方給了一個http介面如下
1、提供介面 http 存取 url,統一字元編碼:UTF-8 。
2、http 介面請求 content-Type="application/json" 。
3、介面透過 http post 請求介面 url 並傳送符合 JSON 規範的 body。
訪問鑰匙也給了,用PHP怎麼實現。謝謝。
請求範例:
{
"secretKey": "**********",
"stationNo": "123456789",
"msgId": "1111",
"data": {
#
"installAddr": "1#熱量表", "accHeat": 11.12, "accCold": 11.13, "flow": 11.15, "accFlow": 11.16,"inTemp": 11.17, "tempDiff": 10.01, "workTime": 12, "workStatus": 0, "coldUnit": "GJ", "powerUnit": "GJ/h" ,
"flowUnit": "m³", //注意單位的'3'為上標
"realFlowUnit": "m³/h"
],
返回範例:
##
NULL2017-12-07 13:37:16
//参数一为提交的地址,参数二为提交方式,参数三为提交数据 function doRequest($url,$method,$data=null){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); if(!empty($data)){ curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; }