goedemiddag/link-header-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

goedemiddag/link-header-parser

Composer 安装命令:

composer require goedemiddag/link-header-parser

包简介

Package for parsing the Link HTTP header

README 文档

README

A PHP package for parsing the HTTP Link header (RFC 8288).

Requirements

This package requires PHP 8.3+.

Installation

You can install the package via composer:

composer require goedemiddag/link-header-parser

Usage

Parse the link header with the LinkHeaderFactory, which will return a LinkHeader object.

use Goedemiddag\LinkHeaderParser\LinkHeaderFactory;

$header = 'Link: <https://api.example.com/items?page=2>; rel="next", <https://api.example.com/items?page=5>; rel="last"';

$linkHeader = LinkHeaderFactory::fromHeader($header);

$next = $linkHeader->getLink('next'); // Link object
$last = $linkHeader->getLink('last'); // Link object
$previous = $linkHeader->getLink('previous'); // null

echo $next->uri; // https://api.example.com/items?page=2

Retrieving links by relation type

Use getLink($rel) to retrieve a Link object by its relation type. If the relation type is not found, it returns null.

$next = $linkHeader->getLink('next'); // Link object, or null

Note:

  • rel="Next" and rel="next" both resolve via getLink('next').
  • When the same rel appears more than once, the last entry wins.

Accessing link attributes

Each Link contains the URI, the rel and any optional extra parameters such as type, title, or hreflang.

$linkHeader = LinkHeaderFactory::fromHeader('<https://example.com/feed>; rel="alternate"; type="application/rss+xml"; title="RSS"');

$link = $linkHeader->getLink('alternate');

echo $link->uri; // https://example.com/feed
echo $link->rel; // alternate
echo $link->getAttribute('type'); // application/rss+xml
echo $link->getAttribute('title'); // RSS
echo $link->getAttribute('missing'); // null

There is also a hasAttribute() helper to check if the Link has an attribute:

$link->hasAttribute('type'); // true
$link->hasAttribute('missing'); // false

Note:

  • Bare token parameters (e.g. ; nocache) have no value and are silently ignored; only name=value pairs are stored as attributes.
  • Attribute names are normalised to lowercase: Type="text/html" is accessible as getAttribute('type').

Example: GitHub pagination

// $response is a PSR-7 ResponseInterface
$linkHeaderValue = $response->getHeaderLine('Link');

$linkHeader = LinkHeaderFactory::fromHeader($linkHeaderValue);

$next = $linkHeader->getLink('next');
if ($next instanceof Link)
    // fetch $next->uri for the next page
}

Contributing

Found a bug or want to add a new feature? Great! There are also many other ways to make meaningful contributions such as reviewing outstanding pull requests and writing documentation. Even opening an issue for a bug you found is appreciated.

When you create a pull request, make sure it is tested, following the code standard (run composer code-style:fix to take care of that for you) and please create one pull request per feature. In exchange, you will be credited as contributor.

Testing

To run the tests, you can use the following command:

composer test

Security

If you discover any security related issues in this or other packages of Goedemiddag, please email dev@goedemiddag.nl instead of using the issue tracker.

About Goedemiddag

Goedemiddag! is a digital web-agency based in Delft, the Netherlands. We are a team of professionals who are passionate about the craft of building digital solutions that make a difference for its users. See our GitHub organisation for more package.

统计信息

  • 总下载量: 147
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固