6 ms·
PHP function to perform a basic cURL request and return the result. Got a little bit tired of writing "curl_setopt(...".
by fuzzmeister 15y ago
PHP function to perform a basic cURL request and return the result. Got a little bit tired of writing "curl_setopt(...".
- dmpatierno 15y agoTypically this is what file_get_contents() is for (with fopen_wrappers enabled) if you just want a simple GET.
- troels 15y agoIn newer versions of PHP, you can use `stream_context_create` to make post requests and more, using `file_get_contents`
- deleted 15y ago[deleted]
- aDemoUzer 15y agoMy solutions: function getUrlContents($url, $showError = true) { for($x = 0; $x < 5 && !$content; $x++) $content = @file_get_contents($url); if(!$content && $showError) br("Unable to retrieve content for '<B>$url</B>'."); return $content; } function getUrlHandle($url, $context = NULL, $showError = true, $retries = 5) { $url = str_replace(' ', '%20', $url); for($x = 0; $x < $retries && !$handle; $x++) if($context) $handle = @fopen($url, 'r', false, $context); else $handle = @fopen($url, 'r'); if(!$handle && $showError) br("\t - Unable to access '<B>$url</B>'."); return $handle; } function br($text = '') { echo $text; if($_SERVER['REQUEST_URI']) echo '<BR>'; echo "\n"; }