POST JSON запрос через CURL и получение ответа от API Яндекс.Вебмастера для сервиса: переобход страниц

// Функция
function curl_zapros_otvet_to_pereobhod ($user_id, $host_id , $token, $url_to_pereobhod){
    $url_to_curl = "https://api.webmaster.yandex.net/v4/user/$user_id/hosts/$host_id/recrawl/queue/";
 
 
    $headers = array(
           'Authorization: OAuth ' . $token,                    // OAuth token. The word Bearer must be used
           "Content-Type: application/json; charset=utf-8"    // Data type and request encoding
        );
 
 
    $data = array('url' => $url_to_pereobhod);
    $json = json_encode($data);
 
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url_to_curl); // На какой адрес посылается запрос
 
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
 
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); // Отправляет Хедер (токен и тип данных)
 
 
    curl_setopt($handle, CURLOPT_POSTFIELDS, $json);  // Отправляет JSON
 
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
 
    $response=curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
 
    return array("code" => $code, "response" => $response);
}
 
 
// Вызов функции
$result = curl_zapros_otvet_to_pereobhod($user_id, $host_id , $token, $url_to_pereobhod);
pr ($result);

 

Как получить $token, $user_id и $host_id я прочёл в статье на хабре.

Если возникнут проблемы, пишите в Telegram (ник: first_Andres)

Связаться с автором

Комментарии

Если у вас есть вопрос, критика или другое мнение - напишите в комментариях.