Yıldırım Sertbaş
Yıldırım Sertbaş

Reputation: 11

How to use Curl to submit URLs İn Bulk With İndexNow API?

You know, with the indexnow API, we can now index pages instantly in Bing and Yandex search engines. I prepared a file like in the documentation (https://www.indexnow.org/documentation), but I don't know with which command to run it from the terminal.

I will be glad if experienced people can help.

Upvotes: 1

Views: 2886

Answers (6)

Alain Bartolo
Alain Bartolo

Reputation: 13

That worked for me :

$url = "https://api.indexnow.org/IndexNow";
$data = array('host'=>'example.com', 'key'=>'2040dfa39465188f6ee8c3d07c052', 'keyLocation'=>'https://example.com/20407aca39465188f6ee8c3d07c052.txt','urlList'=>array('https://example.com/url'));
$content = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

Upvotes: 0

Rowe Morehouse
Rowe Morehouse

Reputation: 4585

Other answers did not work for me, this works for me:

curl -X POST https://api.indexnow.org/IndexNow \
  -H "Content-Type: application/json; charset=utf-8" \
  -d '{
    "host": "example.org",
    "key": "81635xxxxxxa4ac6b2d709e495xxxxxx",
    "keyLocation": "https://example.org/81635xxxxxxa4ac6b2d709e495xxxxxx.txt",
    "urlList": [
      "https://example.org",
      "https://example.org/ur-2",
      "https://example.org/url-3"
    ]
  }'

Upvotes: 1

Ax_
Ax_

Reputation: 997

curl -i POST \
  -H "Content-Type: application/json; charset=utf-8" \
  -d '{
    "host": "example.com", 
    "key":"ffb3f16ace204c8eb58efc30b2e2674d", 
    "keyLocation": "https://example.com/indexnow-key.txt",
    "urlList": [
      "https://example.com/url1",
      "https://example.com/url2"
]}' \
https://www.bing.com/indexnow

Make sure to host the key you generate

Note: Unnecessary use of -X or --request, POST is already inferred.

Upvotes: 1

Full Stack Developer
Full Stack Developer

Reputation: 550

Working code. By using PDO connection fetch all URL and create array, pass it into curl

$pdo = con();
$pattern = 'us';
$sql = "SELECT * FROM big_data WHERE country = :pattern AND id > 68251  ORDER BY id ASC limit 1000";
$statement = $pdo->prepare($sql);
$statement->execute([':pattern' => $pattern]);
$res = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach($res as $r){ 
    $url = url($r['id'], $r['plink'], $r);
    $BLGe[]= 'https://www.haveinlist.com'.$url;
}


$IndexNowKey = "df71a7e2a26a44d89f1a5b6c8d3bbf99"; 
$uRoor = "www.haveinlist.com"; 
$uHttps = "https"; 
$uEngine = "www.bing.com";  
$uRLS = "".$uHttps."://$uRoor/"; 
$data = array('host' => "$uRoor", 'key' => "$IndexNowKey", 'keyLocation' => "".$uRLS."".$IndexNowKey.".txt", 'urlList' =>array_reverse(array_reverse($BLGe)));

$ch = curl_init("https://$uEngine/indexnow");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
$http_code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$res = json_encode($res, JSON_UNESCAPED_UNICODE);
if ($http_code == 200) { echo "Response -$http_code List URL sent successfully"; }
elseif ($http_code == 202) { echo "Response -$http_code <b>Accepted. received, pending verification of key IndexNow</b>"; }
elseif ($http_code == 400) { echo "Response -$http_code <b>Bad request. Bad format</b>"; }
elseif ($http_code == 403) { echo "Response -$http_code <b>Forbidden. Invalid key (e.g. key not found)</b>"; }
elseif ($http_code == 422) { echo "Response -$http_code <b>Unprocessed object. URLs do not belong to host or key does not match scheme in protocol</b>"; }
elseif ($http_code == 429) { echo "Response -$http_code <b>Too many requests. Counted as potential spam</b>"; }
else { echo "$http_code -<b> Error sending URL list</b>"; }

Upvotes: 0

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
//-- Параметры --//
$dlm = "|~|";  // Псевдо разделитель
$dlmBreack = "\n"; // Разделитель
$dlmCount = "10000";  // Ограничение количества считываемых строк из файла
$txtfile = "url.txt"; // Файл данных содержащий список URL-адресов
$IndexNowKey = "6352b4ae24df467dbffe0adb9438cd28"; // Ключ IndoxNew
$uRoor = "example.com"; // Домен
$uHttps = "https"; // http или https
$uEngine = "www.bing.com";  // Кому передаем данные - yandex.com, www.bing.com или api.indexnow.org
//-- Параметры --//
$uRLS = "".$uHttps."://$uRoor/"; // Конечный Url с протоколом http или https
$txtfile = preg_replace("!^s+!m", "", $txtfile); // Обрезать пробелы в начале строки
$txtfile = preg_replace("![\n\r]+\s*[\n\r]+!", "\r\n", $txtfile); // Удаляем пустые строки
$DoLink = file_get_contents($txtfile);
$Link = explode($dlmBreack, $DoLink, $dlmCount);
 for($in = 0; $in < count($Link); $in++){
$Urls = explode($dlm, $Link[$in]);
$uUrl = $Urls[0]; // Строка со ссылкой
if ($uUrl!=='') { $BLGe[]="$uUrl"; } // Создаем массив ссылок
 }
$data = array('host' => "$uRoor", 'key' => "$IndexNowKey", 'keyLocation' => "".$uRLS."".$IndexNowKey.".txt", 'urlList' =>array_reverse(array_reverse($BLGe)));
$data_string = json_encode($data);
$ch = curl_init("https://$uEngine/indexnow");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json; 
charset=utf-8'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
$result = curl_exec($ch);
$http_code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$res = json_encode($res, JSON_UNESCAPED_UNICODE);
 if ($http_code == 200) { echo "<div class=\"alert alert-success text-center\" role=\"alert\">Отет запроса -$http_code <b>Подготовлено и успешно отправлено $count URL</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; }
 elseif ($http_code == 202) { echo "<div class=\"alert alert-primary text-center\" role=\"alert\">Отет запроса -$http_code <b>Подготовлено и успешно отправлено $count URL, ожидается проверка ключа IndexNow</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; }
 elseif ($http_code == 400) { echo "<div class=\"alert alert-danger text-center\" role=\"alert\">Отет запроса -$http_code <b>Неверный запрос. Неверный формат</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; }
 elseif ($http_code == 403) { echo "<div class=\"alert alert-danger text-center\" role=\"alert\">Отет запроса -$http_code <b>Запрещено. Недействительный ключ (например, ключ не найден, файл найден, но ключ отсутствует в файле)</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; }
 elseif ($http_code == 422) { echo "<div class=\"alert alert-danger text-center\" role=\"alert\">Отет запроса -$http_code <b>Необрабатываемый объект. URL-адреса не принадлежат хосту или ключ не соответствует схеме в протоколе</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; }
 elseif ($http_code == 429) { echo "<div class=\"alert alert-danger text-center\" role=\"alert\">Отет запроса -$http_code <b>Слишком много запросов. Расчитывается какпотенциальный спам</b></div><div class=\"alert alert-secondary text-center\" role=\"alert\">сайт - <b>$uHost</b>, запрос - <b>$uEngine</b></div>"; 
}
 else { echo "<div class=\"alert alert-danger text-center\" role=\"alert\">$http_code -<b> Ошибка отправки списка URL</b></div>"; 

?>

Upvotes: 0

Nick Kugaevsky
Nick Kugaevsky

Reputation: 2945

You could use curl from example below if it still actual.

curl -X POST \
  -H "Content-Type: application/json; charset=utf-8" \
  -d '{"host": "example.org", "key":"my-perfect-key", "urlList": ["https://example.org/page1","https://example.org/page2"]}' \
  https://yandex.com/indexnow

Don't forget to place keyfile in the root of your site.

Upvotes: 1

Related Questions