php curl request
function makeCURLRequest($url, $method="GET", $params = "") {
if ($method == "GET" && strpos($url, '?')) {
$urlParams = substr($url, strpos($url, '?')+1);
$url = substr($url, 0, strpos($url, '?'));
if (is_array($params)) {
parse_str($urlParams, $urlParamsArray);
$params = $urlParamsArray + $params;
} else {
$params = $urlParams.'&'.$params;
}
}
if (is_array($params)) {
$params = http_build_query($params,'','&');
}
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
$response = curl_exec($curl);
return $response;
}
if ($method == "GET" && strpos($url, '?')) {
$urlParams = substr($url, strpos($url, '?')+1);
$url = substr($url, 0, strpos($url, '?'));
if (is_array($params)) {
parse_str($urlParams, $urlParamsArray);
$params = $urlParamsArray + $params;
} else {
$params = $urlParams.'&'.$params;
}
}
if (is_array($params)) {
$params = http_build_query($params,'','&');
}
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
$response = curl_exec($curl);
return $response;
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment