由于我的服务器并不支持https,无法接收从小程序提交过来的数据,所以采用这个折中的办法,小程序提交数据=>金数据=>织梦后台。以下是实现此功能的方法。关于如何从小程序提交表单到金数据,可以参考:http://www.ihtmlcss.com/archives/416.html
1.获取表单的json数据:
关于如何打开数据推送功能及获取表单的json数据结构,可参考:http://help.jinshuju.net/articles/http-push.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "form": "7F6DB2", "form_name": "表单名称", "entry": { "serial_number": 123, "field_1": "这是一行文字", "field_2": "13800138000", "field_3": "这是一行文字", "field_4": "选项1", "field_5": "这是一段文字", "creator_name": "老王", "created_at": "2017-06-23 03:08:27 UTC", "updated_at": "2017-06-23 03:08:27 UTC", "info_remote_ip": "127.0.0.1" } } |
2.php代码(注意:无法用$_POST方法获取金数据推送过来的json):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php //获取金数据推送的json并转为数组 $json = file_get_contents('php://input'); $obj = json_decode($json,true); $str = extract($obj); $arr = extract($entry); //读取数组 $name = $entry['field_1']; $tel = $entry['field_2']; $data = $entry['field_3']; $center = $entry['field_4']; $tips = $entry['field_5']; $updatetime = $entry['created_at']; $ipaddress = $entry['info_remote_ip']; //提交数据到织梦自定义表单 $host = 'localhost'; $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $url = '/plus/diy.php'; $param = array( //此处根据自定义表单的结构填写参数 'diyid' => '1', 'do' => '2', 'name' => $name, 'tel' => $tel, 'data' => $data, 'center' => $center, 'tips' => $tips, 'updatetime' => $updatetime, 'ipaddress' => $ipaddress, 'dede_fields' => '表结构', 'dede_fieldshash' => '校检码' ); $data = http_build_query($param); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request $out = "POST ${url} HTTP/1.1\r\n"; $out .= "Host:${host}\r\n"; $out .= "Content-type:application/x-www-form-urlencoded\r\n"; $out .= "Content-length:".strlen($data)."\r\n"; $out .= "Connection:close\r\n\r\n"; $out .= "${data}"; fputs($fp, $out); // get response $response = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); //echo $response; ?> |
文章评论 暂无评论
暂无评论