-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
150 lines (126 loc) · 4.93 KB
/
api.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
header ("Cache-Control: no-cache");
$sandbox = false;
//-----------------REPLACE WITH YOUR CLIENT KEYS----------------------------
$okey=$sandbox?"":"";
//------------------REPLACE THIS KEY WITH YOUR OWN--------------------------
$priv_key=$sandbox?
"-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----":
"-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----";
//I put my keys in serpeate file.
include_once 'keys.php';
//-----------------------GET PARAMETERS---------------------------------------
$callType = "offer";
$postalCode = ($_GET["PostalCode"]!="")?$_GET["PostalCode"]:"19123";
$lat = ($_GET["lat"]!="")?$_GET["lat"]:"39.9643";
$lng = ($_GET["lng"]!="")?$_GET["lng"]:"-75.1444";
$count = ($_GET["count"]!="")?$_GET["count"]:"200";
//----------------------NONCE/TIMESTAMP CREATION------------------------------
$mt = microtime();
$rand = mt_rand();
$nonce = md5($mt.$rand);
$t=time();
//---------------------BASE STRING CREATION-----------------------------------
$base=$sandbox?"GET&https%3A%2F%2Fsandbox.":"GET&https%3A%2F%2F";
$base.="api.mastercard.com%2Foffers%2Fv2%2F" . $callType;
$req_params="";
if ($callType == 'offer') {
$base.="&";
$req_params.="&PostalCode=" . $postalCode;
$req_params.="&Latitude=" . $lat;
$req_params.="&Longitude=" . $lng;
$req_params.="&PageLength=" . $count;
//$req_params.="&SessionKey=" . session_id();
//$req_params.="&PostalCode=63368";
//$req_params.="&Latitude=39.939225";
//$req_params.="&Longitude=75.180352";
//$req_params.="&Category=Arts%20%26%20Entertainment";
$req_params.="&Category=Food%20%26%20Drink";
$req_params.="&PageOffset=0";
//$req_params.="&PageLength=200";
$req_params.="&Format=XML";
$req_params=substr($req_params,1);
}
$params=$req_params."&oauth_nonce=".$nonce;
$params.="&oauth_consumer_key=".$okey;
$params.="&oauth_signature_method=RSA-SHA1";
$params.="&oauth_timestamp=".$t;
$params.="&oauth_version=1.0";
//-------------------LEXICOGRAPHICAL BYTE VALUE ORDERING------------------
$params=ordByteSort($params, "&");
//echo $params;
//------------------------BASE STRING READY-------------------------------
$data=$base.amp($params);
//echo $data;
//------------------------SIGN BASE STRING--------------------------------
openssl_sign($data, $signature, $priv_key);
//---------------------FORMAT BASE STRING---------------------------------
$signature=urlencodeRFC3986(base64_encode($signature));
if(mb_detect_encoding($signature, "UTF-8", true) != "UTF-8" ){$signature = utf8_encode($signature);}
$signature=str_replace("+","%20",$signature);
$signature=str_replace("*","%2A",$signature);
$signature=str_replace("%7E","~",$signature);
//----------------------PREPARE HEADER-------------------------------------
$hdr="OAuth oauth_consumer_key=\"".$okey."\",oauth_nonce=\"".$nonce."\",oauth_timestamp=\"".$t."\",oauth_version=\"1.0\",oauth_signature_method=\"RSA-SHA1\",oauth_signature=\"".$signature."\"";
$header[] = 'Authorization: '.$hdr;
//----------------------SEND REQUEST----------------------------------------
$url=$sandbox?'https://sandbox.api.mastercard.com/offers/v2/'. $callType:'https://api.mastercard.com/offers/v2/'. $callType;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$call = $url.'?'.$req_params;
curl_setopt($ch, CURLOPT_URL, $call);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$begintime = $time;
$result = curl_exec($ch);
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$endtime = $time;
$totaltime = ($endtime - $begintime);
//print_r ($header);
echo 'API call took ' .$totaltime. ' seconds.
';
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
echo $call . '
';
curl_close($ch);
header ("Content-Type:text/xml");
echo $result;
function amp($str){
$str=str_replace('=','%3D',$str);
return str_replace("&","%26",$str);
}
function urlencodeRFC3986($string){return str_replace('%7E', '~', rawurlencode($string));}
//OAuth Lexicographical (Ordinal) Byte Value Ordering.
function ordByteSort($params, $delimiter="&"){
$params = explode($delimiter, $params);
$array = array();
foreach($params as $index => $param){
$keyval = explode("=", $param);
array_push($array, array( 'key' => rawurlencode(trim($keyval[0])),'val' => rawurlencode(trim($keyval[1]) )));
}
$ordBytes = array();
foreach($array as $param) {
$bytes_str ="";
for($i=0;$i<strlen($param['key'].$param['val']);$i++){$chars[$i]=substr($param['key'].$param['val'],$i,1);}
foreach($chars as $chr) {$bytes_str .= dechex(ord($chr));}
array_push($ordBytes, $bytes_str);
}
asort($ordBytes ,SORT_STRING);
$retval = "";
$len = count($array)-1;
foreach($ordBytes as $index=>$value){
$retval .= $array[$index]['key']."=".$array[$index]['val'];
if($len--) $retval .= "&";
}
return $retval;
}
?>