Sample code for 30+ languages & platforms
Zig

Iterate Pages in Feed

See more Facebook Examples

Demonstrates how to read the next page in the user's Facebook feed, and iterates through all of the pages in the Facebook feed.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example assumes a previously obtained an access token
    const oauth2 = try chilkat.OAuth2.init();
    defer oauth2.deinit();
    oauth2.setAccessToken("FACEBOOK-ACCESS-TOKEN");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to Facebook and send the following GET request:
    rest.connect("graph.facebook.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Provide the authentication credentials (i.e. the access key)
    rest.setAuthOAuth2(oauth2) catch {};

    // Gets the 1st page in the user's feed.
    var response_json: [:0]const u8 = rest.fullRequestNoBody(alloc, "GET", "/v2.7/me/feed") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);
    json.load(response_json) catch {};

    // See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.
    //

    const next_url = try chilkat.Url.init();
    defer next_url.deinit();

    // Get the URL for the next page in the feed.
    var next_url_str: [:0]const u8 = try json.stringOf(alloc, "paging.next");
    while (json.getLastMethodSuccess()) {
        std.debug.print("Next page URL: {s}\n", .{next_url_str});

        next_url.parseUrl(next_url_str) catch {};

        // Prepare for getting the next page in the feed.
        // We can continue using the same REST object.
        // If already connected, we'll continue using the existing connection.
        // Otherwise, a new connection will automatically be made if needed.
        rest.clearAllQueryParams() catch {};
        rest.addQueryParams(try next_url.getQuery(alloc)) catch {};

        response_json = rest.fullRequestNoBody(alloc, "GET", "/v2.7/me/feed") catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        json.load(response_json) catch {};
        // See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.

        // Get the URL for the next page.
        next_url_str = try json.stringOf(alloc, "paging.next");
    }

    std.debug.print("No more pages in the feed.\n", .{});
}