php免费调用rapidapi中similarwebapi查询网站域名流量信息代码
代码语言:php
所属分类:其他
代码描述:php免费调用rapidapi中similarwebapi查询网站域名流量信息代码
代码标签: php 免费 调用 rapidapi similarwebapi 查询 网站 域名 流量 信息 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
// =================================================================
// 1. PHP 后端配置与数据处理
// =================================================================
// 获取用户输入的域名,默认为 amazon.com
$domain = isset($_GET['domain']) ? trim($_GET['domain']) : 'https://www.baidu.com';
// API 配置 (填入你的 Key,如果没有 Key,代码会自动使用下方的 Mock 数据)
$apiKey = "YOUR_REAL_API_KEY_HERE"; //去申请https://rapidapi.com/
$apiUrl = "https://similar-web-api.p.rapidapi.com/analyticsv1?url=" . urlencode($domain);
// -----------------------------------------------------------
// 核心逻辑:尝试 API 请求 -> 失败则使用你提供的 JSON 数据
// -----------------------------------------------------------
$jsonResponse = null;
$isDemo = false;
// 1. 尝试初始化 cURL
if ($apiKey !== "YOUR_REAL_API_KEY_HERE") {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
"x-rapidapi-host: similar-web-api.p.rapidapi.com",
"x-rapidapi-key: " . $apiKey
],
]);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode === 200 && $response) {
$jsonResponse = json_decode($response, true);
}
}
// 2. 如果 API 没通或者没配置 Key,使用你提供的 Amazon 数据作为演示
if (!$jsonResponse) {
$isDemo = true;
// 这里是你提供的完整 JSON 数据
$mockData = '{"domain_analytics":{"Version":1,"SiteName":"amazon.com","Description":"Free shipping on millions of items...","TopCountryShares":[{"country":"United States of America","countryCode":"US","countryFlag":"https://purecatamphetamine.github.io/country-flag-icons/3x2/US.svg","Value":0.8621406094076178,"valueinPercentage":"86.21"},{"country":"India","c.........完整代码请登录后点击上方下载按钮下载查看















网友评论0