目前,您可以将电子表格(包含网站列表)导入数据库。您可以单击一个按钮,它会检索 Google 已为数据库中的每个域编制索引的页面数。
它可以正常工作,直到大约 400 个请求,然后我的请求一无所获。不确定我做错了什么或如何使这项工作 - 有什么想法吗?
try {
require('db.php');
$conn = new PDO ( "mysql:host=localhost;dbname=" . $database, $user, $pass );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $conn->query('SELECT `url` FROM domains');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()){
$id = $row['id'];
$url = $row['url'];
$pages = getIndexedPageCount($url);
if ( $pages > 0 ) {
$status = 1;
} else {
$status = 0;
}
$prep = $conn->prepare("
UPDATE `domains`
SET
url = :url,
pages = :pages,
status = :status
WHERE url = :url
");
$prep->execute(array(
':url' => $url,
':pages' => $pages,
':status' => $status
));
}
} catch ( PDOException $e ) {
echo 'Error: ' . $e->getMessage();
exit;
}
function getIndexedPageCount($domain) {
// remove http:// and https://
if ( strpos( $domain, 'http:' ) == 0 ) { $domain = str_replace('http://', '', $domain); }
if ( strpos( $domain, 'https:' ) == 0 ) { $domain = str_replace('https://', '', $domain); }
$content = file_get_contents('https://www.googleapis.com/customsearch/v1?key={{Removed API Key}}&q=site:' . $domain);
if (strlen($content) > 1) {
$data = json_decode($content);
return intval($data->searchInformation->totalResults);
}
else {
echo "Error: URL does not exist.";
}
}
任何帮助将不胜感激; 如果有人有想法,我会尽力让它发挥作用!