@@ -46,18 +46,92 @@ $ composer require jeremykendall/php-domain-parser
46
46
Usage
47
47
--------
48
48
49
+ ### Public suffix resolution.
50
+
51
+ The first objective of the library is using the [ Public Suffix List] ( http://publicsuffix.org/ ) to easily return the ICANN, the Cookie or
52
+ the Private Effective TLD as a ` Pdp\PublicSuffix ` object using the following methods:
53
+
54
+ ~~~ php
55
+ use Pdp\Rules;
56
+
57
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
58
+
59
+ echo $rules->getICANNEffectiveTLD('www.ulb.ac.be'); //display 'ac.be';
60
+ echo $rules->getCookieEffectiveTLD('www.ulb.ac.be'); //display 'ac.be';
61
+ echo $rules->getPrivateEffectiveTLD('www.ulb.ac.be'); //display 'be';
62
+ ~~~
63
+
64
+ The methods are available since version ` 5.7.0 ` to ease the package usage. Prior to this version you could use the
65
+ ` Rules::getPublicSuffix ` method with an optional ` $section ` argument to get the same results:
66
+
67
+ ~~~ php
68
+ use Pdp\Rules;
69
+
70
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
71
+
72
+ echo $rules->getPublicSuffix('www.ulb.ac.be'); // get the cookie effective TLD, display 'ac.be';
73
+ echo $rules->getPublicSuffix('www.ulb.ac.be', Rules::ICANN_DOMAINS); //get the ICANN effective TLD, display 'ac.be';
74
+ echo $rules->getPublicSuffix('www.ulb.ac.be', Rules::PRIVATE_DOMAINS); //get the Private effective TLD, display 'be';
75
+ ~~~
76
+
77
+
78
+ If the Public Suffix is not found or in case of error an exception which extends ` Pdp\Exception ` is thrown.
79
+
80
+ ### Domain resolution.
81
+
82
+ Apart the Public Suffix the package can resolve domain and their information also using Mozilla's [ Public Suffix List] ( http://publicsuffix.org/ )
83
+
84
+ ~~~ php
85
+ use Pdp\Rules;
86
+
87
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
88
+
89
+ echo $rules->resolveCookieDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
90
+ echo $rules->resolveICANNDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
91
+ echo $rules->resolvePrivateDomain('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'be';
92
+ ~~~
93
+
94
+ The methods are available since version ` 5.7.0 ` to ease the package usage. Prior to this version you could use the
95
+ ` Rules::resolve ` method with an optional ` $section ` argument to get the same results:
96
+
97
+ ~~~ php
98
+ use Pdp\Rules;
99
+
100
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat');
101
+
102
+ echo $rules->resolve('www.ulb.ac.be'); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
103
+ echo $rules->resolve('www.ulb.ac.be', Rules::ICANN_DOMAINS); // returns a Pdp\Domain object whose Public Suffix is 'ac.be';
104
+ echo $rules->resolve('www.ulb.ac.be', Rules::PRIVATE_DOMAINS); // returns a Pdp\Domain object whose Public Suffix is 'be';
105
+ ~~~
106
+
107
+ If the Domain is not resolved or in case of error a null ` Pdp\Domain ` is returned.
108
+
109
+ ### Top Level Domains resolution
110
+
111
+ While the [ Public Suffix List] ( http://publicsuffix.org/ ) is a community based list. We can parse the Top Level domain
112
+ information given by the [ IANA website] ( https://data.iana.org/TLD/tlds-alpha-by-domain.txt ) to always resolve
113
+ top domain against the newly registered TLD.
114
+
115
+ ~~~ php
116
+ use Pdp\TopLevelDomains;
117
+
118
+ $rules = TopLevelDomains::createFromPath('/path/to/iana/tlds-alpha-by-domain.txt');
119
+
120
+ echo $rules->resolve('www.UlB.Ac.bE'); //display 'be';
121
+ ~~~
122
+
123
+ If the Domain is not resolved or in case of error a null ` Pdp\Domain ` is returned.
124
+
125
+ ### The Domain and Public Suffix objects.
126
+
49
127
~~~ php
50
128
<?php
51
129
52
- use Pdp\Cache;
53
- use Pdp\CurlHttpClient;
54
- use Pdp\Manager;
55
130
use Pdp\Rules;
56
131
57
- $manager = new Manager(new Cache(), new CurlHttpClient());
58
- $rules = $manager->getRules(); //$rules is a Pdp\Rules object
132
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat'); //$rules is a Pdp\Rules object
59
133
60
- $domain = $rules->resolve ('www.ulb.ac.be'); //$domain is a Pdp\Domain object
134
+ $domain = $rules->resolveICANNDomain ('www.ulb.ac.be'); //$domain is a Pdp\Domain object
61
135
echo $domain->getContent(); // 'www.ulb.ac.be'
62
136
echo $domain->getPublicSuffix(); // 'ac.be'
63
137
echo $domain->getRegistrableDomain(); // 'ulb.ac.be'
@@ -67,14 +141,15 @@ $domain->isKnown(); // returns true
67
141
$domain->isICANN(); // returns true
68
142
$domain->isPrivate(); // returns false
69
143
$domain->labels(); // returns ['be', 'ac', 'ulb', 'www']
70
- $publicSuffix = $rules->getPublicSuffix('mydomain.github.io', Rules::PRIVATE_DOMAINS); //$publicSuffix is a Pdp\PublicSuffix object
144
+
145
+ $publicSuffix = $rules->getPrivateEffectiveTLD('mydomain.github.io'); //$publicSuffix is a Pdp\PublicSuffix object
71
146
echo $publicSuffix->getContent(); // 'github.io'
72
147
$publicSuffix->isKnown(); // returns true
73
148
$publicSuffix->isICANN(); // returns false
74
149
$publicSuffix->isPrivate(); // returns true
75
150
$publicSuffix->labels(); // returns ['io', 'github']
76
151
77
- $altSuffix = $rules->getPublicSuffix ('mydomain.github.io', Rules::ICANN_DOMAINS );
152
+ $altSuffix = $rules->getICANNEffectiveTLD ('mydomain.github.io');
78
153
echo $altSuffix->getContent(); // 'io'
79
154
$altSuffix->isKnown(); // returns true
80
155
$altSuffix->isICANN(); // returns true
@@ -84,7 +159,7 @@ $tldList = $manager->getTLDs(); //$tldList is a Pdp\TopLevelDomains object
84
159
$domain = $tldList->resolve('www.ulb.ac.be'); //$domain is a Pdp\Domain object
85
160
$tldList->contains('be'); //returns true
86
161
$tldList->contains('localhost'); //return false
87
- foreach($tldList as $tld) {
162
+ foreach ($tldList as $tld) {
88
163
//$tld is a Pdp\PublisSuffix object
89
164
}
90
165
~~~
@@ -96,8 +171,7 @@ Using the above code you have parse, validate and resolve a domain name and its
96
171
** Before**
97
172
98
173
~~~ php
99
- $manager = new Manager(new Cache(), new CurlHttpClient());
100
- $rules = $manager->getRules();
174
+ $rules = Rules::createFromPath('/path/to/mozilla/public-suffix.dat'); //$rules is a Pdp\Rules object
101
175
102
176
$domain = $rules->resolve('faß.test.de');
103
177
echo $domain->toAscii()->getContent(); // 'fass.test.de'
@@ -106,19 +180,12 @@ echo $domain->toAscii()->getContent(); // 'fass.test.de'
106
180
** After**
107
181
108
182
~~~ 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
- // );
183
+ $rules = Rules::createFromPath(
184
+ '/path/to/mozilla/public-suffix.dat',
185
+ null, // PHP resource context (see fopen context argument)
186
+ IDNA_NONTRANSITIONAL_TO_ASCII,
187
+ IDNA_NONTRANSITIONAL_TO_UNICODE
188
+ ); //$rules is a Pdp\Rules object
122
189
123
190
$domain = $rules->resolve('faß.test.de');
124
191
echo $domain->toAscii()->getContent(); // 'xn--fa-hia.test.de'
@@ -514,7 +581,7 @@ public Manager::getTLDs(
514
581
): TopLevelDomains
515
582
~~~
516
583
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.
584
+ 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
585
519
586
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
587
0 commit comments