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_SSL_VERIFYPEER => false,
    CURLOPT_HEADER => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 3,
  );
  // 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':
      break;
    case 'POST':
      if(!is_string($data))
        throw new \Exception("Invalid data for cURL request '$method $uri'");
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      break;
    case 'PUT':
      if(!is_string($data))
        throw new \Exception("Invalid data for cURL request '$method $uri'");
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      break;
    case 'DELETE':
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      break;
  }

  // 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));
  //var_dump($raw);
  $lines = explode("\r\n", $raw); // we exploder curl response line by line
  var_dump($lines);
  $headers = array(); 
  $content = '';
  $write_content = false;
  if(count($lines) > 3) {
    foreach($lines as $h) {
      if($h == '')
        $write_content = true;
      else {
        if($write_content)
          $content .= $h."\n";
        else
          $headers[] = $h;
      }
    }
  }
  $error = curl_error($curl);

  curl_close($curl);

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

curlwiz('http://facebook.com', '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:

http://php.net/manual/en/curl.examples-basic.php

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