-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestjson.php
More file actions
56 lines (52 loc) · 1.36 KB
/
testjson.php
File metadata and controls
56 lines (52 loc) · 1.36 KB
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
<?php
define("MO_URL", 'http://10.48.179.115/');
$value='{"status":"+OK","gameid":"308","online":"25","con":"1","e":5}';
// $arr=json_decode($value);
// print $arr->{'a'};
// echo "<br>";
// var_dump($arr);
//json数据转php array
$arr2=json_decode(($value),true);
// echo $arr2['a'];
// echo "<br>";
foreach ($arr2 as $key=>$val){
echo $key." : ".$val." <br>";
}
//php数组转json
$arr=array(
'gameid'=>'305',
'userid'=>'265478',
'chg_grade'=>'-1000',
'chg_esc'=>'-22'
);
$json_data=json_encode($arr);
var_dump($json_data);
print $json_data;
//php curl 方法
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://host/restServer.php',$post_string);
*/
function request_by_curl($remote_server,$post_string){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$remote_server);
curl_setopt($ch,CURLOPT_POSTFIELDS,'datapost='.$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_USERAGENT,"HU-web");
$data = curl_exec($ch);
if ($data == false){
echo 'curl error '.curl_error($ch);
}else {
curl_close($ch);
return $data;
}
// curl_close($ch);
// return $data;
}
$URI='update_grade';
$remote_server=MO_URL.$URI;
echo $remote_server;
request_by_curl($remote_server, $json_data);