34 lines
1.1 KiB
PHP
34 lines
1.1 KiB
PHP
<?php
|
|
// app/helpers/http.php
|
|
function http_post_form($url, array $fields, array $headers = [], $timeout = 10) {
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => http_build_query($fields),
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
CURLOPT_TIMEOUT => $timeout,
|
|
]);
|
|
$body = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
return [$code, $body, $err];
|
|
}
|
|
|
|
function http_post_json($url, array $json, array $headers = [], $timeout = 10) {
|
|
$hdrs = array_merge(['Content-Type: application/json'], $headers);
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($json),
|
|
CURLOPT_HTTPHEADER => $hdrs,
|
|
CURLOPT_TIMEOUT => $timeout,
|
|
]);
|
|
$body = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
return [$code, $body, $err];
|
|
} |