Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shiro_'s avatar

Laravel Add Pagination To External Api Response

Hi, i am trying to consume the marvel api using the server side method. Unfortunately, the api does not have pagination, so i am trying to create that so that i can easily display all pages on the front end.

For now, the data only displays the fist page, and i would like to display data as it is looped from each page.

My code currently looks like this :

public function get_characters($offset = 0)
    {
        $ts = Carbon::now()->toDateString();          
        $private_key =  Config::get('services.marvel_private_key.key');
        $public_key = Config::get('services.marvel_public_key.key');
        $hash = md5($ts . $private_key . $public_key);

        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://gateway.marvel.com:443/v1/public/characters?ts=".$ts."&apikey=".$public_key."&hash=".$hash,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "token" => $hash,
        ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            return $err;
        } else {

            $data = json_decode($response);

            //getting pagination

            $pages = ($data->data->total) / ($data->data->limit);

            $total_pages = round($pages);

            if($total_pages > 1) {

                $current_page = 1;

                //loop through other pages

                for ($i = 1; $i <= $total_pages; $i++) {

                    // echo "page: $i <br>";

                    $curl = curl_init();
                    curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://gateway.marvel.com:443/v1/public/characters?ts=".$ts."&apikey=".$public_key."&hash=".$hash,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                        "token" => $hash,
                    ),
                    ));
                    $response = curl_exec($curl);
                    $err = curl_error($curl);

                    curl_close($curl);

                    if ($err) {
                        return $err;
                    } else {

                        $data = json_decode($response);

                        return $data;
                    }

                }    

            } else {

                return $data;

            }

        }

    }

Any advise or recommendations will be appreciated.

0 likes
1 reply

Please or to participate in this conversation.