-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.php
105 lines (100 loc) · 2.16 KB
/
function.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @author Golga <[email protected]>
* @since 0.0.1 ( 2018-03-06 )
*
* @param String $url
* @param Array|array $fields
* @param Bool|boolean $https
* @param String|null $login
* @return Array
*/
function getCurlData( String $url, Array $fields = [], Bool $https = false, String $login = null )
{
$ch = curl_init();
$data_json = json_encode( $fields );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
// Login HTTP use to access to development serv
if( $login )
{
curl_setopt($ch, CURLOPT_USERPWD, $login);
}
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_json );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HEADER, true); // Display Http header
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
"content-type: application/json",
'content-length: ' . strlen( $data_json ),
"gamifyKey: " . sha1( apiKey . md5( date('Y-m-d') ) )
] );
if ( !$https )
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$chData = curl_exec($ch);
$datas = explode("\r\n\r\n", $chData );
if ( isset( $datas[2] ) )
{
/*
* Handdle HTTP 100 & 300
*/
foreach ( $datas as $key => $data )
{
if( json_decode( $data ) !== null )
{
$ret["header"] = $datas[ (int) $key -1 ];
$ret["body"] = $datas[ (int) $key ];
}
}
}
elseif( isset( $datas[1] ) )
{
$ret["header"] = $datas[0];
$ret["body"] = $datas[1];
}
else
{
$ret["body"] = $datas[0];
}
$ret["full"] = $chData;
return $ret;
}
/**
* @author Golga <[email protected]>
* @since 0.0.1 ( 2018-03-29 )
*
* @param Array $data
* @param Bool $ful
* @return Void
*/
function diplayGamifyCallBack( Array $data, Bool $full = false )
{
$dataJson = json_decode( $data["body"] );
if ( isset( $dataJson->success ) )
{
$class = "success";
}
else
{
$class = "error";
}
echo "<div class='api-data " . $class . "'>";
if ( $full )
{
echo "<pre>" . $data["full"] . "</pre>";
}
else
{
echo "<h3>Header : </h3>";
if ( isset( $data["header"] ) )
{
echo "<pre>" . $data["header"] . "</pre>";
}
echo "<pre>" . $data["header"] . "</pre>";
echo "<h4>Body : </h4>";
echo "<pre>" . $data["body"] . "</pre>";
}
echo "</div>";
}
?>