How to Update API file

This example shows how to check version of API file and how to update it.
In return command of first function write right url of your PAP.
 
<?php

//base url of your pap
function getPapUrl() {
    return 'https://www.yoursite.com/affiliate/';
}

//name of api file (with path if it is not in the same directory as this script)
function getApiFilename() {
    return 'PapApi.class.php';
}

function getVersionFromPap() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, getPapUrl().'api/version.php');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $error = curl_error($ch);

    curl_close($ch);
    if (strlen($error)) {
        echo 'Curl error: ' . $error . '; ' . getPapUrl().'api/version.php';
        die();
    }
    preg_match('(\s*.*)', $response, $matches);
    preg_match('(\d.*<)', $matches[0] , $matches);
    return substr($matches[0], 0, -1);
}

function getVersionFromLocalFile() {
    $fileHandler = @fopen(getApiFilename(), 'r');

    while (($buffer = @fgets($fileHandler)) !== false) {
        if(preg_match('(PAP version:.*,)', $buffer , $matches)) {
             fclose($fileHandler);
             return substr($matches[0], 13, -1);
        }
    }
    return null;
}

function updatePapApiFile() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, getPapUrl().'api/download.php');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $papApiSourcecode = curl_exec($ch);
    $error = curl_error($ch);

    curl_close($ch);
    if (strlen($error)) {
        echo 'Curl error: ' . $error . '; ' . getPapUrl().'api/version.php';
        die();
    }
    file_put_contents(getApiFilename(), $papApiSourcecode);
}

function isUpdateNeeded() {
    return is_null(getVersionFromLocalFile()) || getVersionFromLocalFile() != getVersionFromPap();
}


if (isUpdateNeeded()) {
    updatePapApiFile();
}

?>
×