Skip to content

Instantly share code, notes, and snippets.

@amans2k
Created November 30, 2017 10:24
Show Gist options
  • Save amans2k/449740b545aef7321b8d95ea868cdde7 to your computer and use it in GitHub Desktop.
Save amans2k/449740b545aef7321b8d95ea868cdde7 to your computer and use it in GitHub Desktop.
This function fires a curl request with get or post method
/*
* This function fires a curl request with get or post method
* $method contains 'get' or 'post'
* $query_string contains $query_string = "?x=10&y=20";
*/
curl_download($Url, $query_string, $method);
function curl_download($Url, $query_string, $method) {
if ($method == 'get') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url . $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$response = trim(curl_exec($ch));
$err = curl_error($curl);
curl_close($ch);
if ($err) {
$result = $err;
} else {
$result = $response;
}
return $result;
} elseif ($method == 'post') {
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
$response = trim(curl_exec($ch));
$err = curl_error($curl);
curl_close($ch);
if ($err) {
$result = $err;
} else {
$result = $response;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment