Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/test.php
<?php

define('CLIENT_ID', '18cc42cb777a4af389e6619f154d1a3c'); // wprowadź Client_ID aplikacji
define('CLIENT_SECRET', '8jaMsSxzCvIzStImmfPfzjTiDoJ71WsctIdvBD3LL6pz4RwGyNl8nMjMEbdSUfgF'); // wprowadź Client_Secret aplikacji

function getCurl($headers, $url, $content = null) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    if ($content !== null) {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    }
    return $ch;
}

function getAccessToken() {
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
    $content = "grant_type=client_credentials";
    $url = "https://allegro.pl/auth/oauth/token";
    $ch = getCurl($headers, $url, $content);
    $tokenResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($tokenResult === false || $resultCode !== 200) {
        exit ("Something went wrong");
    }
    return json_decode($tokenResult)->access_token;
}

function getAccessUser() {
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
    $content = "grant_type=client_credentials";
    $url = "https://allegro.pl/auth/oauth/authorize";
    $ch = getCurl($headers, $url, $content);
    $tokenResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($tokenResult === false || $resultCode !== 200) {
        exit ("Something went wrong");
    }
    return json_decode($tokenResult)->access_token;
}

function getMainCategories($token) {
    $headers = array("Authorization: Bearer {$token}", "Accept: application/vnd.allegro.public.v1+json");
    $url = "https://api.allegro.pl/sale/categories";
    $ch = getCurl($headers, $url);
    $mainResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($mainResult === false || $resultCode !== 200) {
        exit ("Something went wrong");
    }
    return json_decode($mainResult);
}

function getDeliveryMethod($token) {
    $headers = array("Authorization: Bearer {$token}", "Accept: application/vnd.allegro.public.v1+json");
    $url = "https://api.allegro.pl/parcel-management/delivery-services";
    $ch = getCurl($headers, $url);
    $mainResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($mainResult === false || $resultCode !== 200) {
        exit ("Something went wrong ".$resultCode);
    }
    return json_decode($mainResult);
}

function getOffers($token) {
    $headers = array("Authorization: Bearer {$token}", "Accept: application/vnd.allegro.public.v1+json");
    $url = "https://api.allegro.pl/sale/offers";
    $ch = getCurl($headers, $url);
    $mainResult = curl_exec($ch);
    print_r($mainResult);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($mainResult === false || $resultCode !== 200) {
        exit ("Something went wrong ".$resultCode);
    }
    return json_decode($mainResult);
}

 function main()
{
    print_r(getAccessUser());
}

main();