badraxas / adstxt
PHP 广告txt生成器是一个简单易用的库,允许你在PHP应用程序中处理授权数字卖家(ads.txt)文件格式。
v3.0.4
2024-01-23 23:39 UTC
Requires
- php: >=8.1
- ext-mbstring: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.22
- phpunit/phpunit: ^10.3
- symfony/var-dumper: ^6.3
- vimeo/psalm: ^5.13
README
目录
简介
PHP AdsTxt解析器是一个简单易用的库,允许你在PHP应用程序中处理授权数字卖家(ads.txt)文件格式。
安装
您可以通过Composer安装此库。在项目目录中运行以下命令:
composer require badraxas/adstxt
用法
从文件解析AdsTxt
<?php use Badraxas\Adstxt\AdsTxtParser; try { $adsTxt = (new AdsTxtParser())->fromFile('/path/to/ads.txt'); // You can now work with the $adsTxt instance containing the parsed data. } catch (\Badraxas\Adstxt\Exceptions\AdsTxtParser\FileOpenException $exception) { // Handle the file open exception here. }
从字符串解析AdsTxt
<?php use Badraxas\Adstxt\AdsTxt; use Badraxas\Adstxt\AdsTxtParser; $adsTxtContent = <<<'EOD' # Example ads.txt content example.com, 123456, DIRECT, ABCD1234 domain.com, 987654, RESELLER custom_variable=custom_value # This is a comment EOD; $adsTxt = (new AdsTxtParser())->fromString($adsTxtContent); // Now you have an instance of AdsTxt containing the parsed data from the ads.txt string. // You can use the $adsTxt object to perform various operations on the ads.txt data.
从URL解析AdsTxt
<?php use Badraxas\Adstxt\AdsTxtFetcher; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; // Assuming $client and $requestFactory are instances of ClientInterface and RequestFactoryInterface $fetcher = new AdsTxtFetcher($client, $requestFactory); try { $adsTxt = $fetcher->fromUrl('https://example.com/ads.txt'); // You can now work with the $adsTxt instance containing the parsed data from the URL. } catch (\Badraxas\Adstxt\Exceptions\AdsTxtParser\UrlOpenException $exception) { // Handle the URL open exception here. }
PSR-18和PSR-17兼容性
为确保互操作性和符合标准HTTP消息,AdsTxtFetcher类需要一个符合PSR-18的HTTP客户端(ClientInterface)和一个符合PSR-17的HTTP请求工厂(RequestFactoryInterface)。
此设计选择允许灵活地将AdsTxtFetcher与符合这些PSR标准的各种HTTP客户端实现集成,确保广泛的兼容性,并且可以轻松地根据需要交换不同的客户端实现。
处理AdsTxt实例
<?php use Badraxas\Adstxt\AdsTxt; use Badraxas\Adstxt\Enums\Relationship; use Badraxas\Adstxt\Lines\Record; use Badraxas\Adstxt\Lines\Comment; use Badraxas\Adstxt\Lines\Variable; // Assuming $adsTxt is an instance of AdsTxt $invalidLines = $adsTxt->getInvalidLines(); $isAdsTxtValid = $adsTxt->isValid(); // Add custom filtering using callback $filteredAdsTxt = $adsTxt->filter(function ($line) { // Your custom filtering logic here return $line instanceof Record; // Return true if the line should be included, false otherwise }); // Compare the current AdsTxt instance with another AdsTxt instance and return the lines that are missing. * in the other instance $otherAdsTxt = AdsTxtParser::fromFile('/path/to/other_ads.txt'); $missingLines = $adsTxt->diff($otherAdsTxt); // Create new AdsTxt and add lines $newAdsTxt = new AdsTxt(); $newAdsTxt ->addLine(new Comment(' app-ads.txt file for vMVPD B:')) ->addLine(new Record('ssp.com', 'vwxyz', 'DIRECT')) ->addLine(new Variable('inventorypartnerdomain', 'programmerA.com')) // display ads.txt as string $newAdsTxt->__toString();
可用的行类型
供应商行
供应商行代表包含供应商信息的ads.txt文件中的行。
<?php use Badraxas\Adstxt\Lines\Record; use Badraxas\Adstxt\Enums\Relationship; // Creating a Vendor line instance $vendorLine = new Record( domain: 'example.com', publisherId: '123456', relationship: 'DIRECT', certificationId: 'ABCD1234', comment: null );
变量行
变量行代表包含变量及其名称和值的ads.txt文件中的行。
<?php use Badraxas\Adstxt\Lines\Variable; // Creating a Variable line instance $variableLine = new Variable( name: 'custom_variable', value: 'custom_value', comment: null );
注释行
注释行代表ads.txt文件中的注释行。
<?php use Badraxas\Adstxt\Lines\Comment; // Creating a Comment line instance $commentLine = new Comment('This is a comment');
注意:您可以将注释实例与任何供应商、变量或无效实例相关联。
use Badraxas\Adstxt\Lines\Comment; use Badraxas\Adstxt\Lines\Variable; new Variable( 'variable', 'value', new Comment('This is a comment') );
无效行
无效行代表ads.txt文件中的无效行。
<?php use Badraxas\Adstxt\Lines\Invalid; // Creating an Invalid line instance $invalidLine = new Invalid('Invalid content');
空白行
空白行代表ads.txt中的空白行。
<?php use Badraxas\Adstxt\Lines\Blank; // Creating an Invalid line instance $blankLine = new Blank();