-
Notifications
You must be signed in to change notification settings - Fork 6
Implement API consumption with pagination for posts #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,4 +33,57 @@ public function actionIndex(){ | |||||
| public function actionTest1(){ | ||||||
| return $this->render('test1'); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Displays Test Posts Francisco Girona | ||||||
| * @return array $posts | ||||||
| */ | ||||||
| public function actionPosts(){ | ||||||
|
|
||||||
| $url = 'https://jsonplaceholder.typicode.com/posts'; | ||||||
|
|
||||||
| $context = stream_context_create([ | ||||||
| 'http' => [ | ||||||
| 'method' => 'GET', | ||||||
| 'timeout' => 5, | ||||||
| 'header' => "Accept: application/json\r\n", | ||||||
| ], | ||||||
| 'ssl' => [ | ||||||
| 'verify_peer' => true, | ||||||
| 'verify_peer_name' => true, | ||||||
| ], | ||||||
| ]); | ||||||
|
|
||||||
| $raw = @file_get_contents($url, false, $context); | ||||||
|
|
||||||
| if($raw === false){ | ||||||
| return $this->render('posts', [ | ||||||
| 'data' => [], 'error' => 'Failed to fetch data from the API' | ||||||
| ]); | ||||||
| } | ||||||
|
|
||||||
| $posts = json_decode($raw, true); | ||||||
|
|
||||||
| if(!is_array($posts)){ | ||||||
| return $this->render('posts', [ | ||||||
| 'data' => [], 'error' => 'Invalid data format' | ||||||
| ]); | ||||||
| } | ||||||
|
|
||||||
| // Pagination | ||||||
| $totalPosts = count($posts); | ||||||
| $postsPerPage = 10; | ||||||
| $totalPages = ceil($totalPosts / $postsPerPage); | ||||||
| $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The page parameter from
Suggested change
|
||||||
|
|
||||||
| // Control Get Parameters page is not valid | ||||||
| if($currentPage < 1 || $currentPage > $totalPages){ | ||||||
| return $this->render('posts', ['data' => [], 'error' => 'Invalid page number', 'totalPages' => $totalPages, 'currentPage' => $currentPage]); | ||||||
| } | ||||||
|
|
||||||
| $offset = ($currentPage - 1) * $postsPerPage; | ||||||
| $posts = array_slice($posts, $offset, $postsPerPage); | ||||||
|
|
||||||
| return $this->render('posts', ['data' => $posts, 'error' => '', 'totalPages' => $totalPages, 'currentPage' => $currentPage]); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** @var yii\web\View $this */ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** @var array $data */ | ||||||||||||||||||||||||||||||
| /** @var string|null $error */ | ||||||||||||||||||||||||||||||
| use yii\helpers\Url; | ||||||||||||||||||||||||||||||
| use yii\helpers\Html; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $this->title = 'Posts — JSONPlaceholder'; | ||||||||||||||||||||||||||||||
| $this->params['breadcrumbs'][] = $this->title; | ||||||||||||||||||||||||||||||
| ?> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| <?php if($error): ?> | ||||||||||||||||||||||||||||||
| <div class="alert alert-danger"> | ||||||||||||||||||||||||||||||
| <?= Html::encode($error) ?> | ||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||
| <?php endif; ?> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| <?php if($data): ?> | ||||||||||||||||||||||||||||||
| <div class="posts"> | ||||||||||||||||||||||||||||||
| <h1>Posts</h1> | ||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||
| <?php endif; ?> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| <?php foreach($data as $post): ?> | ||||||||||||||||||||||||||||||
| <article class="post"> | ||||||||||||||||||||||||||||||
| <h2 class="post-title"><?= Html::encode($post['title']) ?></h2> | ||||||||||||||||||||||||||||||
| <p class="post-body"><?= Html::encode($post['body']) ?></p> | ||||||||||||||||||||||||||||||
| </article> | ||||||||||||||||||||||||||||||
| <?php endforeach; ?> | ||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no check to ensure
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| <?php if($totalPages > 1): ?> | ||||||||||||||||||||||||||||||
| <div class="pagination"> | ||||||||||||||||||||||||||||||
| <?php for($i = 1; $i <= $totalPages; $i++): ?> | ||||||||||||||||||||||||||||||
| <a href="<?= Url::to(['site/posts', 'page' => $i]) ?>" class="pagination-link <?= $currentPage == $i ? 'active' : '' ?>" aria-label="Page <?= $i ?>" title="Page <?= $i ?>"><?= $i ?></a> | ||||||||||||||||||||||||||||||
| <?php endfor; ?> | ||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||
| <?php endif; ?> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using the
@error suppression operator withfile_get_contents(). It's better to handle errors explicitly using try/catch or by checking error conditions witherror_get_last()after the operation.