<?php
require get_template_directory() . '/inc/zebra_curl.php';

function getBlogPosts(){

// include the library
// (you don't need this if you installed the library via Composer)

// instantiate the Zebra cURL class
$curl = new Zebra_cURL();

// cache results 18.000 (5 hours) seconds
$curl->cache(get_template_directory() . "/api-cache", 60 * 300);

// since we are communicating over HTTPS, we load the CA bundle from the examples folder,
// so we don't get CURLE_SSL_CACERT response from cURL
// you can always update this bundle from https://curl.haxx.se/docs/caextract.html
$curl->ssl(false);

// a simple way of scrapping a page
// (you can do more with the "get" method and callback functions)

    $url = get_ayar("blog_url");
    $post_type = get_ayar("blog_post_type");
    $request_url = $url . "/wp-json/wp/v2/" . $post_type;
    $thumb_request_url = $url . "/wp-json/wp/v2/media/";
    $cat_request_url = $url . "/wp-json/wp/v2/categories/";

    $return = $curl->scrap($request_url."?per_page=".get_ayar("blog_post_count"), true);

    $data = json_decode($return);
    $final_data = array();
    $i = 0;

    foreach($data as $final){

    $media_id = $final->featured_media;
    $media_req = $thumb_request_url.$media_id;
        if($media_id == 0){
            $thumb = "";
        }else{
            $thumb = json_decode($curl->scrap($media_req, true))->media_details->sizes->medium->source_url;
        }

    $final_data[$i]["post_title"] = $final->title->rendered;
    $final_data[$i]["post_thumbnail"] = $thumb;
    $final_data[$i]["post_link"] = $final->link;
    $final_data[$i]["post_content"] = $final->content->rendered;
    $final_data[$i]["post_category"] = json_decode($curl->scrap($cat_request_url."?post=".$final->id, true))[0]->name;
    
    $i++;
    }
    return $final_data;
}
@x1881; @emirkrgz;