@@ -27,7 +27,7 @@ System Requirements
27
27
28
28
You need:
29
29
30
- - ** PHP >= 7.0 ** but the latest stable version of PHP is recommended
30
+ - ** PHP >= 7.2.5 ** but the latest stable version of PHP is recommended
31
31
- the ` intl ` extension
32
32
33
33
Dependencies
@@ -46,18 +46,63 @@ $ composer require jeremykendall/php-domain-parser
46
46
Usage
47
47
--------
48
48
49
+ ### Domain part resolutions
50
+
51
+ The first objective of the library is using the [ Public Suffix List] ( http://publicsuffix.org/ ) to easily resolve domain information.
52
+
49
53
~~~ php
50
- <?php
54
+ use Pdp\Rules;
51
55
52
- use Pdp\Cache;
53
- use Pdp\CurlHttpClient;
54
- use Pdp\Manager;
56
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
57
+
58
+ echo $rules->getCookieDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
59
+ echo $rules->getICANNDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
60
+ echo $rules->getPrivateDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'be';
61
+ ~~~
62
+
63
+ ** WARNING: If the Domain can not be resolved an exception is thrown.**
64
+
65
+ These methods are available since version ` 5.7.0 ` to ease the package usage. Prior to this version you could use the
66
+ ` Rules::resolve ` method with an optional ` $section ` argument to get the same results:
67
+
68
+ ~~~ php
55
69
use Pdp\Rules;
56
70
57
- $manager = new Manager(new Cache(), new CurlHttpClient());
58
- $rules = $manager->getRules(); //$rules is a Pdp\Rules object
71
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
72
+
73
+ echo $rules->resolve('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
74
+ echo $rules->resolve('www.ulb.ac.be', Rules::ICANN_DOMAINS); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
75
+ echo $rules->resolve('www.ulb.ac.be', Rules::PRIVATE_DOMAINS); // returns a Pdp\Domain object whose Public Suffix is 'be';
76
+ ~~~
77
+
78
+ ** WARNING: If the Domain can not be resolved or in case of error a null ` Pdp\Domain ` is returned.**
79
+
80
+ ### Top Level Domains resolution
81
+
82
+ While the [ Public Suffix List] ( http://publicsuffix.org/ ) is a community based list. We can parse the Top Level domain
83
+ information given by the [ IANA website] ( https://data.iana.org/TLD/tlds-alpha-by-domain.txt ) to always resolve
84
+ top domain against the newly registered TLD.
59
85
60
- $domain = $rules->resolve('www.ulb.ac.be'); //$domain is a Pdp\Domain object
86
+ ~~~ php
87
+ use Pdp\TopLevelDomains;
88
+
89
+ $rules = TopLevelDomains::createFromPath('/path/to/iana/tlds-alpha-by-domain.txt');
90
+
91
+ echo $rules->resolve('www.UlB.Ac.bE')->getPublicSuffix(); //display 'be';
92
+ ~~~
93
+
94
+ If the Domain is not resolved or in case of error a null ` Pdp\Domain ` is returned.
95
+
96
+ ### The Domain and Public Suffix objects.
97
+
98
+ ~~~ php
99
+ <?php
100
+
101
+ use Pdp\Rules;use Pdp\TopLevelDomains;
102
+
103
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat'); //$rules is a Pdp\Rules object
104
+
105
+ $domain = $rules->getICANNDomain('www.ulb.ac.be'); //$domain is a Pdp\Domain object
61
106
echo $domain->getContent(); // 'www.ulb.ac.be'
62
107
echo $domain->getPublicSuffix(); // 'ac.be'
63
108
echo $domain->getRegistrableDomain(); // 'ulb.ac.be'
@@ -67,24 +112,12 @@ $domain->isKnown(); // returns true
67
112
$domain->isICANN(); // returns true
68
113
$domain->isPrivate(); // returns false
69
114
$domain->labels(); // returns ['be', 'ac', 'ulb', 'www']
70
- $publicSuffix = $rules->getPublicSuffix('mydomain.github.io', Rules::PRIVATE_DOMAINS); //$publicSuffix is a Pdp\PublicSuffix object
71
- echo $publicSuffix->getContent(); // 'github.io'
72
- $publicSuffix->isKnown(); // returns true
73
- $publicSuffix->isICANN(); // returns false
74
- $publicSuffix->isPrivate(); // returns true
75
- $publicSuffix->labels(); // returns ['io', 'github']
76
-
77
- $altSuffix = $rules->getPublicSuffix('mydomain.github.io', Rules::ICANN_DOMAINS);
78
- echo $altSuffix->getContent(); // 'io'
79
- $altSuffix->isKnown(); // returns true
80
- $altSuffix->isICANN(); // returns true
81
- $altSuffix->isPrivate(); // returns false
82
-
83
- $tldList = $manager->getTLDs(); //$tldList is a Pdp\TopLevelDomains object
115
+
116
+ $tldList = TopLevelDomains::createFromPath('/path/to/IANA/RootZoneDatabase.txt'); //$tldList is a Pdp\TopLevelDomains object
84
117
$domain = $tldList->resolve('www.ulb.ac.be'); //$domain is a Pdp\Domain object
85
118
$tldList->contains('be'); //returns true
86
119
$tldList->contains('localhost'); //return false
87
- foreach($tldList as $tld) {
120
+ foreach ($tldList as $tld) {
88
121
//$tld is a Pdp\PublisSuffix object
89
122
}
90
123
~~~
@@ -96,8 +129,7 @@ Using the above code you have parse, validate and resolve a domain name and its
96
129
** Before**
97
130
98
131
~~~ php
99
- $manager = new Manager(new Cache(), new CurlHttpClient());
100
- $rules = $manager->getRules();
132
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat'); //$rules is a Pdp\Rules object
101
133
102
134
$domain = $rules->resolve('faß.test.de');
103
135
echo $domain->toAscii()->getContent(); // 'fass.test.de'
@@ -106,19 +138,12 @@ echo $domain->toAscii()->getContent(); // 'fass.test.de'
106
138
** After**
107
139
108
140
~~~ php
109
- $manager = new Manager(new Cache(), new CurlHttpClient());
110
- $rules = $manager->getRules()
111
- ->withAsciiIDNAOption(IDNA_NONTRANSITIONAL_TO_ASCII)
112
- ->withUnicodeIDNAOption(IDNA_NONTRANSITIONAL_TO_UNICODE);
113
-
114
- // or
115
- //
116
- // $rules = $manager->getRules(
117
- // Manager::PSL_URL,
118
- // null,
119
- // IDNA_NONTRANSITIONAL_TO_ASCII,
120
- // IDNA_NONTRANSITIONAL_TO_UNICODE
121
- // );
141
+ $rules = Rules::createFromPath(
142
+ '/path/to/mozilla/public-suffix.dat',
143
+ null, // PHP resource context (see fopen context argument)
144
+ IDNA_NONTRANSITIONAL_TO_ASCII,
145
+ IDNA_NONTRANSITIONAL_TO_UNICODE
146
+ ); //$rules is a Pdp\Rules object
122
147
123
148
$domain = $rules->resolve('faß.test.de');
124
149
echo $domain->toAscii()->getContent(); // 'xn--fa-hia.test.de'
@@ -514,7 +539,7 @@ public Manager::getTLDs(
514
539
): TopLevelDomains
515
540
~~~
516
541
517
- These methods returns a ` Pdp\Rules ` or ` Pdp\TopLevelDomains ` objects seeded with their corresponding data fetch from the cache or from the external resources depending on the submitted ` $ttl ` argument.
542
+ These methods return a ` Pdp\Rules ` or ` Pdp\TopLevelDomains ` objects seeded with their corresponding data fetch from the cache or from the external resources depending on the submitted ` $ttl ` argument.
518
543
519
544
These methods take an optional ` $url ` argument which specifies the PSL source URL. If no local cache exists for the submitted source URL, the method will:
520
545
@@ -538,7 +563,7 @@ They are used when instantiated the returned object.
538
563
539
564
~~~ php
540
565
$manager = new Pdp\Manager(new Pdp\Cache(), new Pdp\CurlHttpClient());
541
- $tldCollection = $manager->getTLDs(self ::RZD_URL);
566
+ $tldCollection = $manager->getTLDs(Pdp\Manager ::RZD_URL);
542
567
$domain = $tldCollection->resolve('www.ulb.ac.be');
543
568
echo $domain->getPublicSuffix(); // print 'be'
544
569
~~~
0 commit comments