php Google Indexing API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Google Indexing API相关的知识,希望对你有一定的参考价值。
<?php
// ** Report errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// ** Load dependencies
require_once "vendor/autoload.php";
// ** Connect to Database
$config = parse_ini_file(__DIR__ . "./../../config.ini", true);
$database_address = "localhost";
$database_name = $config["database"]["name"];
$database_user = $config["database"]["user"];
$database_pass = $config["database"]["pass"];
$table_name = "dk_lever_jobs";
$db = new PDO("mysql:host=$database_address;dbname=$database_name;charset=UTF8", $database_user, $database_pass, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
// ** Get Job IDs from Job Feed
$job_url = "https://api.lever.co/v0/postings/[CLIENT_NAME]?mode=json";
$open_stream = file_get_contents($job_url);
$job_data = json_decode($open_stream);
$feed_job_ids = array();
foreach($job_data as $job):
$feed_job_ids[] = $job->id;
endforeach;
// ** Get Job IDs from Database
$db_job_ids = array();
$query = $db->prepare("SELECT job_id FROM $table_name");
$query->execute();
$results = $query->fetchAll();
foreach ($results as $row):
$row = (object) $row; // make into object
$db_job_ids[] = $row->job_id;
endforeach;
// ** Get Difference between DB Jobs and Feed Jobs
// Output will be Jobs no longer active
$deleted_jobs = array_diff($db_job_ids, $feed_job_ids);
// ** Create Array of Jobs to Delete
$jobs_to_delete = array();
foreach($deleted_jobs as $id):
$query = $db->prepare("SELECT title FROM $table_name WHERE job_id = '$id' ");
$query->execute();
$row = $query->fetch();
$jobs_to_delete[] = array(
"id" => $id,
"title" => $row['title'],
);
endforeach;
// ** Authentication with OAuth
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('service_account_file.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// ** Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// ** Google Index API Post Request
if( !empty($jobs_to_delete) ):
foreach($jobs_to_delete as $job):
$job = (object) $job; // make an object
$title = str_replace(array(" ", ","), "-", strtolower($job->title)); // format title
$url = "https://careers.google.com/job?id={$job->id}&title={$title}";
// ** Define contents here.
$content = '{
"url": "' . $url . '",
"type": "URL_DELETED"
}';
// ** Make Request
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
// echo $status_code; // for debugging
// echo $response->getBody(); // for debugging
endforeach;
endif;
以上是关于php Google Indexing API的主要内容,如果未能解决你的问题,请参考以下文章
通过“google-api-php-client”库检索 Google Search Analytics
Google Calendar API 批量请求 PHP
如何检查Google客户端API密钥是否对Google PHP API库有效?
使用 gmail-api 和 google-api-php-client 发送电子邮件
php [appscript api] google app script api
php [gsearch console] google搜索控制台api #google