Simple PHP CURL method

Here is a simple PHP method for consuming a different APIs

As you can see, the method supports cookies, XML requests, basic HTTP Auth, etc….

Leave a comment if you need help or you have a question

/**
 * Send CURL request
 * @param string $apiUrl
 * @param string $apiMethod GET, POST, DELETE, PUT
 * @param array $apiParams
 * @param array $apiHeader Accept: application/json, Content-type: application/x-www-form-urlencoded, X-API-Token: ApiKey,..
 *              application/json, application/xml, text/html, text/plain
 * @param bool $debug
 * @param string $cookieFile
 * @param bool $cookieResume
 * @param bool $followRedirect
 * @param string $xml
 * @param string $dPost
 * @return array $result['httpCode'], $result['responseHeader'], $result['responseBody']
 */
function curlRequest($apiUrl, $apiMethod, $apiParams, $apiHeader, $debug = false, $cookieFile = '',
                     $cookieResume = false, $followRedirect = false, $xml = '', $dPost = '')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
    if ($cookieFile != '') {
        curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/" . $cookieFile);
    }
 
    if ($cookieResume) {
        curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/" . $cookieFile);
    }
 
    switch ($apiMethod) {
        case 'DELETE':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));
            break;
 
        case 'PUT':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));
            break;
 
        case 'GET':
            if (count($apiParams) > 0) {
                $apiUrl .= '?' . http_build_query($apiParams);
            }
            break;
 
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, TRUE);
            if (count($apiParams) > 0) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));
            }
            if ($dPost != '') {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $dPost);
            }
            if ($xml != '') {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            }
            break;
 
        case 'HTTPAuth':
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $apiParams['login'] . ":" . $apiParams['password']);
            break;
    }
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $apiHeader);
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    if ($followRedirect) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    }
    $response = curl_exec($ch);
 
    if ($debug) {
        var_dump($response);
    }
 
    $responseInfo = curl_getinfo($ch); // header_size , http_code
    curl_close($ch);
 
    $responseHeader = trim(substr($response, 0, $responseInfo['header_size']));
    $responseBody = substr($response, $responseInfo['header_size']);
 
    $result['httpCode'] = $responseInfo['http_code'];
    $result['responseHeader'] = $responseHeader;
    $result['responseBody'] = $responseBody;
 
    return $result;
}

GET request sample

// GET
$ret = curlRequest('https://someurl/api', 'GET', [], ['Accept: application/json'], false);
echo $ret['httpCode'] . "\n";
echo $ret['responseHeader'] . "\n";
echo $ret['responseBody'] . "\n";

POST request sample

// POST
$apiParamsArr = [
    'param1' => 'something',
    "param2" => 5,
    "param3" => "another string"
];
 
$ret = curlRequest('https://someurl/api', 'POST', $apiParamsArr, ['Accept: application/json'], true);
echo $ret['httpCode'] . "\n";
echo $ret['responseHeader'] . "\n";
echo $ret['responseBody'] . "\n";

DELETE request sample

$ret = curlRequest('https://someurl/api/107', 'DELETE', [], ['Accept: application/json'], false);
echo $ret['httpCode'] . "\n";
echo $ret['responseHeader'] . "\n";
echo $ret['responseBody'] . "\n";

Leave a Reply

Your email address will not be published. Required fields are marked *