PHP code example of badraxas / adstxt

1. Go to this page and download the library: Download badraxas/adstxt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

badraxas / adstxt example snippets




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.
}

 

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.



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.
}



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 ))

// display ads.txt as string
$newAdsTxt->__toString();



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
);



use Badraxas\Adstxt\Lines\Variable;

// Creating a Variable line instance
$variableLine = new Variable(
  name: 'custom_variable',
  value: 'custom_value',
  comment: null
);



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')
);



use Badraxas\Adstxt\Lines\Invalid;

// Creating an Invalid line instance
$invalidLine = new Invalid('Invalid content');



use Badraxas\Adstxt\Lines\Blank;

// Creating an Invalid line instance
$blankLine = new Blank();