Categories: Development

Ultimate PHP cURL function example

The ultimate all in one PHP cURL function that saves the hassle of building a requester/setter/getter script that is cURL.

The idea behind the function was to cater for a variety of options whilst still being simple to set and run.

An example of this is a simple GET request that has no authentication, all you need to set is the URL like:

doCurl('https://theurl.com/api/request');

Or you can use the full parameters to set headers, user agents and referrers.

With doCurl() you can:

  1. Send GET, POST and DELETE requests
  2. Set and include headers
  3. Set post fields
  4. Set user agent
  5. Set referrer
  6. Set follow request
  7. Set use SSL
  8. Set connection timeout
  9. Set timeout

Upon a successful response code of 200 the data response will be returned, else an array stating the non 200 response will be returned.

Code Gist:

<?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_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
    }
}

 

Share
Tags: PHPWeb Dev

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago