Created
November 14, 2021 09:30
-
-
Save AucT/ab0a0fd12c785f132885300b444a9c20 to your computer and use it in GitHub Desktop.
indexnow php example bing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IndexNow | |
{ | |
const LOG = true; | |
public static function submit(array $urls): void | |
{ | |
$data = [ | |
'host' => self::config('host'), | |
'key' => self::config('key'), | |
'keyLocation' => self::config('keyLocation'), | |
'urlList' => $urls | |
]; | |
$data = json_encode($data); | |
$ch = curl_init('https://www.bing.com/IndexNow'); | |
curl_setopt_array($ch, [ | |
CURLOPT_POSTFIELDS => $data, | |
CURLOPT_HTTPHEADER => ['Content-Type: application/json; charset=utf-8'], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYHOST => 0, | |
CURLOPT_SSL_VERIFYPEER => 0 | |
]); | |
$result = curl_exec($ch); | |
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if ($http_code != 200 && self::LOG) { | |
self::log("IndexNow Error http_code $http_code $result. \nsubmitted data:$data"); | |
} | |
} | |
private static function log(string $text): void | |
{ | |
//TODO Change this to your log method | |
echo date('Y-m-d H:i:s') . " $text" . PHP_EOL; | |
} | |
private static function config(string $name): ?string | |
{ | |
//TODO Change this to get config stuff from your config file if you care about privacy | |
$myConfig = [ | |
'host' => 'www.example.org', | |
'key' => '9a38b010d7d3453cb3c70c06d457c20b', | |
'keyLocation' => 'https://www.example.org/9a38b010d7d3453cb3c70c06d457c20b.txt', | |
]; | |
return $myConfig[$name] ?? null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment