PHP cURL wrappers to consume APIs, crawl and more…

cURL might be confusing for many, but it is matter of learning the basics on how to config the options, knowing which method (GET, POST, PUT) but..

Why in the hell would you use curl for? 

  • Requests to API’s sending parameters and stuff. Consume them easily.
  • Requests to websites, download and stuff.
  • Crawl websites parsing DOM and following links.
  • many more..
// the best fuction so you stop saving time.
function curlwiz($uri, $method='GET', $data=null, $curl_headers=array(), $curl_options=array()) {
  // default curl options which will almost be static, you can modify if you want
  $default_curl_options = array(
    CURLOPT_HEADER => true,
  // you can set the default headers into this array, usually you dont need them.
  $default_headers = array();

  // We need to trim and change into MAYUS the method passed
  $method = strtoupper(trim($method));
  $allowed_methods = array('GET', 'POST', 'PUT', 'DELETE'); // array with allowed methods. 

  if(!in_array($method, $allowed_methods)) // if the method from input is not in allowed_methods array, then throw an error.
    throw new \Exception("'$method' is not valid cURL HTTP method.");

  if(!empty($data) && !is_string($data))
    throw new \Exception("Invalid data for cURL request '$method $uri'");

  // init
  $curl = curl_init($uri);

  // apply default options
  curl_setopt_array($curl, $default_curl_options);

  // apply method specific options
  switch($method) {
    case 'GET':
    case 'POST':
        throw new \Exception("Invalid data for cURL request '$method $uri'");
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    case 'PUT':
        throw new \Exception("Invalid data for cURL request '$method $uri'");
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    case 'DELETE':
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

  // apply user options
  curl_setopt_array($curl, $curl_options);

  // add headers
  curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($default_headers, $curl_headers));

  // parse result from curl
  $raw = rtrim(curl_exec($curl));
  $lines = explode("\r\n", $raw); // we exploder curl response line by line
  $headers = array(); 
  $content = '';
  $write_content = false;
  if(count($lines) > 3) {
    foreach($lines as $h) {
      if($h == '')
        $write_content = true;
      else {
          $content .= $h."\n";
          $headers[] = $h;
  $error = curl_error($curl);


  // return
  return array(
    'raw' => $raw,
    'headers' => $headers,
    'content' => $content,
    'error' => $error

curlwiz('', 'GET');

With this you will be able to easily do do cURL just using 1 line of code and pre-configured set up of cURL into a function and using switch in the function. So we can split to know which configuration needs to be done if GET, or which other with POST.

There are many guides on how to config cURL but there is one better than all guides:

But there are also plenty of wrappers built by many other coders, but now you understand how they create so easy wrappers, many using OOP and many others with functions and switch (which I like much more).

List of wrappers i found:

With that! you will be able to code like this lazy fat:

Leave a Reply