Created
December 13, 2017 23:18
-
-
Save rquast/434faef84db143a7d2d9f664c4b5b375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function postJSON($url, $data, $method = 'POST') { | |
$data_string = str_replace("\r", '', json_encode($data)); | |
$headers = [ | |
'Accept: application/json', | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string) | |
]; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . DEFAULT_ZONE_MTA_HOST . ':' . DEFAULT_ZONE_MTA_PORT . $url); | |
curl_setopt($ch, CURLOPT_PORT, DEFAULT_ZONE_MTA_PORT); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
if ($method !== 'POST') { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
$response = json_decode(curl_unescape($ch, curl_exec($ch)), true); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return [ | |
'response' => $response, | |
'status' => $status_code | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment