-
-
Save medamin25/c6344eeb5e7841f557bd51659195a34c to your computer and use it in GitHub Desktop.
Ultimate PHP cURL function example
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
<?php | |
function doCurl(string $url, string $type = 'GET', array $headers = [], array $post_fields = [], string $user_agent = '', string $referrer = '', bool $follow = true, bool $use_ssl = false, int $con_timeout = 10, int $timeout = 40) | |
{ | |
$crl = curl_init($url); | |
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, $type); | |
curl_setopt($crl, CURLOPT_USERAGENT, $user_agent); | |
curl_setopt($crl, CURLOPT_REFERER, $referrer); | |
if ($type == 'POST') { | |
curl_setopt($crl, CURLOPT_POST, true); | |
if (!empty($post_fields)) { | |
curl_setopt($crl, CURLOPT_POSTFIELDS, $post_fields); | |
} | |
} | |
if (!empty($headers)) { | |
curl_setopt($crl, CURLOPT_HTTPHEADER, $headers); | |
} | |
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, $follow); | |
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $con_timeout); | |
curl_setopt($crl, CURLOPT_TIMEOUT, $timeout); | |
curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, $use_ssl); | |
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, $use_ssl); | |
curl_setopt($crl, CURLOPT_ENCODING, 'gzip,deflate'); | |
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); | |
$call_response = curl_exec($crl); | |
$http_response_code = curl_getinfo($crl, CURLINFO_HTTP_CODE); | |
curl_close($crl); | |
if ($http_response_code == 200) { | |
return $call_response;//Return data | |
} else { | |
return array('http_response_code' => $http_response_code);//Call failed | |
} | |
} |
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
<?php | |
function doCurl(string $url, string $type = 'GET', array $headers = [], array $post_fields = [], string $user_agent = '', string $referrer = '', bool $follow = true, bool $use_ssl = false, int $con_timeout = 10, int $timeout = 40) | |
{ | |
$crl = curl_init($url); | |
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, $type); | |
curl_setopt($crl, CURLOPT_USERAGENT, $user_agent); | |
curl_setopt($crl, CURLOPT_REFERER, $referrer); | |
if ($type == 'POST') { | |
curl_setopt($crl, CURLOPT_POST, true); | |
if (!empty($post_fields)) { | |
curl_setopt($crl, CURLOPT_POSTFIELDS, $post_fields); | |
} | |
} | |
if (!empty($headers)) { | |
curl_setopt($crl, CURLOPT_HTTPHEADER, $headers); | |
} | |
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, $follow); | |
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $con_timeout); | |
curl_setopt($crl, CURLOPT_TIMEOUT, $timeout); | |
curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, $use_ssl); | |
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, $use_ssl); | |
curl_setopt($crl, CURLOPT_ENCODING, 'gzip,deflate'); | |
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); | |
$call_response = curl_exec($crl); | |
curl_close($crl); | |
return $call_response;//Return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment