发布于 2026-01-06 0 阅读
0

如何将 dev.to 上的帖子加载到您的网站/应用中

如何将 dev.to 上的帖子加载到您的网站/应用中

在一段时间没有维护网站后,我重新开始维护,发现之前我一直手动编写指向我在开发平台上发布的博客文章的链接。这样做既不规范,代码库看起来也不美观,而且继续这样做效率很低。

我的网站是用PHP开发的,我使用的是Pico CMS ,它有一个非常好的插件设置,所以我决定为Pico创建一个新的插件,以便通过编程方式获取文章,而不是继续以前那种硬编码的疯狂做法。

为了简化可能以前没有使用过 PICO 之类的工具的读者,我将只展示不使用任何框架的常规 PHP 实现。

在我们开始之前

要跟随以下步骤操作并使用以下实现,您首先需要获取DEV API的 API 密钥。获取密钥非常简单,请在开始获取密钥之前按照以下步骤操作:

  1. 前往您的个人资料设置页面
  2. 请输入您需要 API 密钥的用途描述。
  3. 点击Generate API Key
  4. 复制生成的密钥
  5. 搞定啦😁

DevPostFetcher 类

/**
 * DevPostFetcher - A class to fetch posts from your dev.to profile
 * @author James Robb
 * @version 1.0.0
 */
class DevPostFetcher
{
  /** @var int The page of posts to access */
  private $page = 1;
  /** @var int How many posts to provide per page request */
  private $per_page = 10;
  /** @var string dev.to API Key to access posts */
  private $api_key;

  public function __construct(string $api_key) {
    $this->api_key = $api_key;
  }

  /**
   * @param   int   $per_page The amount of posts to fetch per page request
   * @return  void
   */
  public function setPerPage(int $per_page): void {
    $this->per_page = $per_page;
  }

  /**
   * @return  int
   */
  public function getPerPage(): int {
    return $this->per_page;
  }

  /**
   * @param   int   $page  The page of posts to fetch
   * @return  void
   */
  public function setPage(int $page): void {
    $this->page = $page;
  }

  /**
   * @return  int
   */
  public function getPage(): int {
    return $this->page;
  }

  /**
   * @throws Exception if the DEV API curl request fails
   * @return array An associative (key => value) array of DEV posts
  */
  public function fetch() {
    $page = $this->getPage();
    $per_page = $this->getPerPage();
    $api_key = $this->api_key;
    $ch = curl_init(
      "https://dev.to/api/articles/me?page=$page&per_page=$per_page"
    );
    $requestHeaders = [
      "api-key:$api_key"
    ];

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
    $posts = curl_exec($ch);
    if (curl_errno($ch)) {
      throw new Exception(curl_error($ch));
    }
    curl_close($ch);
    return json_decode($posts, true);
  }
}
Enter fullscreen mode Exit fullscreen mode

这门课围绕三个私有属性展开:

  1. $page我们想要访问的帖子页面
  2. $per_page每个页面请求要加载多少篇文章
  3. $api_key- 我们之前生成的 API 密钥

$page我们还为设置提供了 getter 和 setter $per_page,例如,这些对于分页非常有用,我们将在本文后面看到。

附注

有时您可能会遇到与本地计算机上的 SSL 证书相关的 curl 错误,要解决这些错误,您可以将以下代码行添加到fetch()方法中:

  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Enter fullscreen mode Exit fullscreen mode

请勿在生产环境中使用这些:仅限本地开发环境使用,因为这是一个安全问题,您不希望在生产服务器上传入和传出不安全的请求。

使用类

最新帖子

假设我们只想获取您帐户中的最新 10 条帖子,并且您想显示它们,我们可以这样做,例如:

<?php
  $devPosts = new DevPostFetcher("your_api_key");
  $devPostsList = $devPosts->fetch();
?>
<ul>
  <?php foreach($devPostsList as $post): ?>
    <li>
      <a href="<?php echo $post['url']; ?>">
        <?php echo $post['title']; ?>
      </a>
    </li>
  <?php endforeach; ?>
</ul>

Enter fullscreen mode Exit fullscreen mode

分页帖子

假设我们想要对文章进行分页,这也很容易:

<?php
  $devPosts = new DevPostFetcher("your_api_key");
  $firstPagePosts = $devPosts->fetch(); // posts 1 - 10
  $devPosts->setPage(2);
  $secondPagePosts = $devPosts->fetch(); // posts 11 - 20
?>
Enter fullscreen mode Exit fullscreen mode

注意:在本系列的下一篇文章中,我们将使用Composer 类DevPostFetcherFlight构建一个分页文章应用程序

加载超过默认帖子数

你可能已经猜到了,如果你想每次获取超过 10 篇文章,我们可以这样做:

<?php
  $devPosts = new DevPostFetcher("your_api_key");
  $devPosts->setPerPage(20);
  $firstPagePosts = $devPosts->fetch(); // posts 1 - 20
?>
Enter fullscreen mode Exit fullscreen mode

把所有东西整合起来

一个完整的例子可能是这样的:

<?php
$devPosts = new DevPostFetcher("your_api_key");
$devPosts->setPerPage(5);
$devPosts->setPage(2);
$devPostsList = $devPosts->fetch();
?>
<ul>
  <?php foreach($devPostsList as $post): ?>
    <li>
      <a href="<?php echo $post['url']; ?>">
        <?php echo $post['title']; ?>
      </a>
    </li>
  <?php endforeach; ?>
</ul>
Enter fullscreen mode Exit fullscreen mode

综上所述

DEV API虽然基础但根据我这次的使用体验来看,它已经相当完善了,为什么不亲自去体验一下呢?

本系列文章将继续介绍如何使用DevPostFetcher本文中的类、ComposerFlight构建分页博客应用程序。

文章来源:https://dev.to/jamesrweb/programmatically-loading-posts-from-dev-to-onto-your-site-10je