Skip to content

Implement API consumption with pagination for posts#9

Open
frangirona86 wants to merge 1 commit intoooptimo:mainfrom
frangirona86:franciscogirona
Open

Implement API consumption with pagination for posts#9
frangirona86 wants to merge 1 commit intoooptimo:mainfrom
frangirona86:franciscogirona

Conversation

@frangirona86
Copy link
Copy Markdown

What I implemented

I completed PHP Test by consuming the JSONPlaceholder API and displaying the posts on a paginated list.

Main features:

  • Fetches posts from the API endpoint provided
  • Shows 10 posts per page with title and body
  • Added pagination links at the bottom
  • Handles errors when the API fails or returns invalid data
  • Validates the page parameter to avoid crashes

Technical choices:

I used file_get_contents() with a custom stream context instead of cURL because it's simpler for this use case. Added a 5-second timeout and SSL verification for security.

For the view, I used Html::encode() to prevent XSS attacks and structured the posts with <article> tags for better semantics.

The CSS is minimal, focusing on clean post presentation and functional pagination.

Let me know if you need any clarifications or changes!

Copy link
Copy Markdown

@windsurf-bot windsurf-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other comments (3)
  • controllers/SiteController.php (73-85) The implementation fetches all posts from the API and then applies pagination locally. For real-world APIs with large datasets, this approach is inefficient. Consider using the API's built-in pagination parameters (if available) to request only the needed subset of data.
  • views/site/posts.php (3-8) The variables `$totalPages` and `$currentPage` are used in the pagination section but aren't documented in the PHPDoc at the top of the file. Consider adding them to the documentation for better code clarity:
    /** @var yii\web\View $this */
    
    /** @var array $data */
    /** @var string|null $error */
    /** @var int $totalPages */
    /** @var int $currentPage */
    use yii\helpers\Url;
    
  • controllers/SiteController.php (38-40) The method docblock incorrectly states the return type is `array $posts`, but the method actually returns the result of `$this->render()` which is a string/response.
         * Displays Test Posts Francisco Girona
         * @return string
         */
    

💡 To request another review, post a new comment with "/windsurf-review".

Comment on lines +26 to +31
<?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; ?>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no check to ensure $data is an array before the foreach loop. If $data is null or not an array, this could cause a PHP error. Consider adding a check or using empty() instead:

Suggested change
<?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; ?>
<?php if(!empty($data)): ?>
<?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; ?>
<?php endif; ?>

$totalPosts = count($posts);
$postsPerPage = 10;
$totalPages = ceil($totalPosts / $postsPerPage);
$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page parameter from $_GET should be validated to ensure it's numeric before casting to integer. Consider using filter_input() or checking with is_numeric() first.

Suggested change
$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$currentPage = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;

],
]);

$raw = @file_get_contents($url, false, $context);
Copy link
Copy Markdown

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 with file_get_contents(). It's better to handle errors explicitly using try/catch or by checking error conditions with error_get_last() after the operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant