[exec]
function PostRequest($url, $data_object = null, $username = null, $password = null)
{
$curl = curl_init($url);
$data_json = json_encode($data_object, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘.strlen($data_json)
));
header(‘Access-Control-Allow-Origin: *’);
if ($_SERVER[‘REQUEST_METHOD’] === ‘OPTIONS’) {
header(‘Access-Control-Allow-Origin: *’);
header(‘Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS’);
header(‘Access-Control-Allow-Headers: token, Content-Type’);
header(‘Access-Control-Max-Age: 1728000’);
header(‘Content-Length: 0’);
header(‘Content-Type: text/plain’);
die();
}
if ($username && $password)
{
curl_setopt($curl, CURLOPT_USERPWD, $username.”:”.$password);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error)
{
throw new \Exception($error, 500);
}
$result = json_validate($response);
return $result;
};
function json_validate($string)
{
if ($string == null || !is_string($string) || $string == “”)
{
return null;
}
$result = json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ”;
break;
case JSON_ERROR_DEPTH:
$error = ‘The maximum stack depth has been exceeded.’;
break;
case JSON_ERROR_STATE_MISMATCH:
$error = ‘Invalid or malformed JSON.’;
break;
case JSON_ERROR_CTRL_CHAR:
$error = ‘Control character error, possibly incorrectly encoded.’;
break;
case JSON_ERROR_SYNTAX:
$error = ‘Syntax error, malformed JSON.’;
break;
case JSON_ERROR_UTF8:
$error = ‘Malformed UTF-8 characters, possibly incorrectly encoded.’;
break;
case JSON_ERROR_RECURSION:
$error = ‘One or more recursive references in the value to be encoded.’;
break;
case JSON_ERROR_INF_OR_NAN:
$error = ‘One or more NAN or INF values in the value to be encoded.’;
break;
case JSON_ERROR_UNSUPPORTED_TYPE:
$error = ‘A value of a type that cannot be encoded was given.’;
break;
default:
$error = ‘Unknown JSON error occured.’;
break;
}
if ($error)
{
throw new \Exception($error, 500);
}
return $result;
};
$request = array(
“key” => “OlB7bF4tb0woISgoaX07RTlbWmpzKmcsM3MyMzFzWjE/fTJPQV5OeHw3YiFxZGdzMjMxMFQmRl93Iw==”,
“zip” => $_POST[‘zip’]
);
$result = PostRequest(“https://tennispronow.com/api/index.php”, $request);
print_r(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
[/exec]