-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebsiteCompanyName.php
175 lines (154 loc) · 4.72 KB
/
WebsiteCompanyName.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\HttpClient\HttpClient;
class WebsiteCompanyName
{
protected $prefixes = [ 'http://www.', 'http://', 'https://www.', 'https://', ];
protected $browser;
protected $crawler;
protected $guessing_methods = [
'title',
'og_site_name',
'og_title',
// 'local_business',
'organization',
'copyright',
];
public $guesses = [];
public $domain;
public $sld;
public $name;
function __construct($domain)
{
$parsed_domain = (object) parse_url($domain);
if(isset($parsed_domain->path)){
$this->domain = str_replace('www.', '', $parsed_domain->path);
} elseif(isset($url->host)){
$this->domain = str_replace('www.', '', $parsed_domain->host);
}
$this->sld = explode('.', $this->domain)[0];
$this->name = ucwords(str_replace('-', ' ', $this->sld));
$this->browser = new HttpBrowser(HttpClient::create());
try {
foreach ($this->prefixes as $prefix) {
$this->crawler = $this->browser->request('GET', $prefix . $this->domain, [
'max_redirects' => 3,
]);
if ($this->browser->getResponse()->getStatusCode() == 200) {
break;
}
}
foreach ($this->guessing_methods as $method) {
foreach ($this->split($this->{$method}()) as $guess) {
array_push($this->guesses, trim($guess));
}
}
} catch (\GuzzleHttp\Exception\ConnectException $e) {
array_push($this->guesses, trim($this->name));
} catch (\GuzzleHttp\Exception\RequestException $e) {
array_push($this->guesses, trim($this->name));
} catch (\InvalidArgumentException $e) {
array_push($this->guesses, trim($this->name));
}
}
function title()
{
return $this->crawler->filter('title')->text();
}
function og_site_name()
{
$element = $this->crawler->filter('meta[property="og:site_name"]')->eq(0);
if (count($element)) {
return $element->attr('content');
}
}
function og_title()
{
$element = $this->crawler->filter('meta[property="og:title"]')->eq(0);
if (count($element)) {
return $element->attr('content');
}
}
// function local_business()
// {
// $element = $this->crawler->filter('[itemtype="http://schema.org/LocalBusiness"] [itemprop="name"]')->eq(0);
// if (count($element)) {
// return $element->text();
// }
// }
function organization()
{
$element = $this->crawler->filter('[type="application/ld+json"]')->eq(0);
if (count($element)) {
$json = json_decode($element->text(), true);
if (isset($json['@type'], $json['name']) && $json['@type'] == 'Organization') {
return $json['name'];
}
}
}
// function organization()
// {
// $element = $this->crawler->filter('[itemtype="http://schema.org/Organization"] [itemprop="name"]')->eq(0);
// if (count($element)) {
// return $element->text();
// }
// }
function copyright()
{
$copyright = $this->crawler->filter('p, span, div, li, small')->each(function (Crawler $node, $i) {
if (str_contains($node->text(), '©')) {
return $node->text();
}
});
$copyright = array_filter($copyright);
$copyright = array_pop($copyright);
$copyright = str_ireplace([
'copyright',
'©',
'2025',
'2024',
'2023',
'2022',
'2021',
'2020',
'2019',
'2018',
'2017',
'2016',
'2015',
'All Rights Reserved',
'All Right Reserved',
'.',
], '', $copyright);
return $copyright;
}
function guess()
{
$this->guesses = array_filter($this->guesses);
$this->guesses = array_unique($this->guesses);
$this->guesses = array_values($this->guesses);
if (empty($this->guesses)) {
array_push($this->guesses, $this->name);
}
$scores = [];
foreach ($this->guesses as $guess) {
similar_text($this->sld, $guess, $score);
$scores[$guess] = $score;
}
asort($scores);
$scores = array_keys($scores);
return array_pop($scores);
}
function split($text)
{
return preg_split('/–|-|,|\\|•|•|:|\.|\|/', $text ?? '');
}
function __toString()
{
return json_encode([
'best_guess' => $this->guess(),
'guesses' => $this->guesses,
]);
}
}