-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathIndexing.php
88 lines (77 loc) · 1.66 KB
/
Indexing.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
<?php
namespace Melbahja\Seo;
use Melbahja\Seo\Interfaces\SeoInterface;
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class Indexing implements SeoInterface
{
/**
* website hostname.
* @var string
*/
protected $host;
/**
* Engines api keys.
* @var array
*/
protected $keys = [];
/**
* Initialize indexer.
* @param string $host
* @param array $keys
*/
public function __construct(string $host, array $keys)
{
$this->host = $host;
$this->keys = $keys;
}
/**
* Instant index single url.
*
* @param string $url
* @return array
*/
public function indexUrl(string $url): array
{
return $this->indexUrls([$url]);
}
/**
* Instant index multiple urls.
*
* @param array $urls
* @return array
*/
public function indexUrls(array $urls): array
{
$accepted = [];
foreach($this->keys as $engine => $key)
{
$accepted[$engine] = $this->index($engine, $key, $urls);
}
return $accepted;
}
/**
* Send index request to search engine.
*
* @param string $engine
* @param string $apiKey
* @param array $urls
* @return bool
*/
protected function index(string $engine, string $apiKey, array $urls): bool
{
$ch = curl_init("https://{$engine}/indexnow");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['host' => $this->host, 'key' => $apiKey, 'urlList' => $urls]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $code >= 200 && $code < 300;
}
}