proxycheck / proxycheck-php
proxycheck.io PHP 库
Requires
- php: >=5.4
- ext-curl: *
- ext-json: *
README
这是一个调用 proxycheck.io v2 API 的 PHP 库,允许您检查 IP 地址是否为代理或 VPN,并获取被检查 IP 地址的国家、ASN 和提供商。此库还支持检查电子邮件地址,以确定它们是否属于一次性电子邮件服务。
通过 Composer 安装
您可以通过运行以下命令使用 Composer 安装此库:Composer
composer require proxycheck/proxycheck-php
要使用此库,请使用 Composer 的自动加载
require_once('vendor/autoload.php');
依赖项
为了正常运行,该库需要以下扩展:
curl
(如果您打算使用 TLS 查询,请确保您的cacert.pem 已更新)json
服务限制
- 没有 API 密钥的免费用户 = 每日 100 次查询
- 有 API 密钥的免费用户 = 每日 1,000 次查询
- 有 API 密钥的付费用户 = 每日 10,000 到 10.24 百万+ 次查询
在 proxycheck.io 获取您的 API 密钥,这是免费的。
入门指南
对地址进行检查(支持 IPv4 和 IPv6 IP 地址以及用于一次性邮箱检查的电子邮件地址)。
// Get your visitors IP address or email address // If you're using CloudFlare change $_SERVER["REMOTE_ADDR"] to $_SERVER["HTTP_CF_CONNECTING_IP"] // You may also supply an array of addresses in $address to check multiple addresses at once. $address = $_SERVER["REMOTE_ADDR"]; // Input your options for this query including your optional API Key and query flags. $proxycheck_options = array( 'API_KEY' => '######-######-######-######', // Your API Key. 'ASN_DATA' => 1, // Enable ASN data response. 'DAY_RESTRICTOR' => 7, // Restrict checking to proxies seen in the past # of days. 'VPN_DETECTION' => 1, // Check for both VPN's and Proxies instead of just Proxies. 'RISK_DATA' => 1, // 0 = Off, 1 = Risk Score (0-100), 2 = Risk Score & Attack History. 'INF_ENGINE' => 1, // Enable or disable the real-time inference engine. 'TLS_SECURITY' => 0, // Enable or disable transport security (TLS). 'QUERY_TAGGING' => 1, // Enable or disable query tagging. 'MASK_ADDRESS' => 1, // Anonymises the local-part of an email address (e.g. anonymous@domain.tld) 'CUSTOM_TAG' => '', // Specify a custom query tag instead of the default (Domain+Page). 'BLOCKED_COUNTRIES' => array('Wakanda', 'WA'), // Specify an array of countries or isocodes to be blocked. 'ALLOWED_COUNTRIES' => array('Azeroth', 'AJ') // Specify an array of countries or isocodes to be allowed. ); $result_array = \proxycheck\proxycheck::check($address, $proxycheck_options);
在上面的示例中,我们已将国家名称和 isocodes 包含在 BLOCKED_COUNTRIES
和 ALLOWED_COUNTRIES
字段中。这是因为从 0.1.3 版本(2019 年 5 月)开始,此库现在支持这两者用于这些数组中。您可以将这两个字段视为类似于本地白名单/黑名单功能,但仅针对国家。
查看查询结果
在执行查询时,您将收到一个包含各种信息的数组。下面是一个示例,说明如何解析该数组以确定是否应阻止该用户。请注意,block 和 block_reason 结果仅在检查单个地址时填充,当传递地址数组以执行多检查时,这两个变量都将包含 na
。
if ( $result_array['block'] == "yes" ) { // Example of a block and the reason why. echo "Blocked, reason: " . $result_array['block_reason']; exit; } else { // No Proxy / VPN / Blocked Country detected. echo "Not blocked."; }
查询结果中包含的额外信息
在执行查询时,您将收到的不仅仅是 block: yes/no
和 block_reason: [reason]
,还有来自 proxycheck.io 的完整 API 响应,我们这样做是为了让您能够轻松构建阻止系统或根据您的需要利用 API 提供的数据。以下是一个完整的示例。
Array ( [status] => ok/warning/denied/error [node] => answering_node_name [###.###.###.###] => Array ( [asn] => AS##### [range] => ###.###.###.###/24 [hostname] => 78-2-adsl.acme.net [provider] => Acme Incorporated [organisation] => Acme Net [country] => Wakanda [isocode] => WA [region] => Wakanda North [regioncode] => WAN [city] => Birnin Zana [postcode] => BZ967 [latitude] => 2.5072 [longitude] => -0.1276 [currency] => Array ( [code] => VD [name] => Vibranium Dollar [symbol] => $ ) [proxy] => yes/no [type] => VPN/SOCKS5/SOCKS4/SOCKS/HTTP/HTTPS/Compromised Server [risk] => 0 to 100 [port] => ##### [last seen human] => 6 hours, 18 minutes, 49 seconds ago [last seen unix] => 1528687645 ) [block] => yes/no [block_reason] => proxy/vpn/country )
在上面的示例中,status
字段让您了解查询的状态。您可以在我们的 API 文档页面 这里 查看所有我们的 API 响应。此外,在我们的示例中,我们显示 ###.###.###.###
,您将收到实际发送到 API 进行检查的实际地址。
从您的仪表板查看统计信息
在 0.1.3 版本(2019 年 5 月)中,我们添加了通过此库从您的帐户仪表板查看统计信息的功能。下面是如何做到这一点的示例。
$proxycheck_options = array( 'API_KEY' => '', // Your API Key. 'TLS_SECURITY' => 0, // Enable or disable transport security (TLS). 'STAT_SELECTION' => 'usage', // Stats to view: detections, usage or queries 'LIMIT' => '10', // Specify how many entries to view (applies to detection stats only) 'OFFSET' => '0' // Specify an offset in the entries to view (applies to detection stats only) ); $result_array = \proxycheck\proxycheck::stats($proxycheck_options);
访问仪表板API时,始终需要API密钥,因为仪表板仅对注册用户开放(免费和付费用户均可完全访问仪表板)。您还需要确保在网站上启用了仪表板API访问权限(在您的仪表板中)。
您可以在STAT_SELECTION
字段中看到,我们提供了我们希望查看的统计信息的名称。在这个例子中,我们选择了usage
,但您也可以检查检测或查询。此库还将自动支持未来的统计信息类型。
下面是此结果的外观示例。
Array ( [Queries Today] => 234 [Daily Limit] => 1000 [Queries Total] => 840931 [Plan Tier] => Free )
当查看detections
统计信息时,您还可以提供一个限制(即显示给您的条目数量)和一个偏移量(即开始显示结果之前跳过的条目数量)。
例如,如果您有100条条目,您只想查看31至40条条目,则应提供10作为限制,30作为偏移量。
操作您的CORS源、白名单或黑名单
在版本0.1.2(2018年11月)中,我们添加了通过此库查看、添加、删除、设置和清除您白名单和黑名单的功能,到2020年12月,proxycheck.io添加了CORS API,以下代码也适用于该API。以下是在单个查询中添加三个白名单条目的示例。
$proxycheck_options = array( 'API_KEY' => '', // Your API Key. 'TLS_SECURITY' => 0, // Enable or disable transport security (TLS). 'LIST_SELECTION' => 'whitelist', // Specify the list you're accessing: CORS, whitelist or blacklist 'LIST_ACTION' => 'add', // Specify an action: list, add, remove, set or clear. 'LIST_ENTRIES' => array('8.8.8.8', '1.1.1.1/24', 'AS888') // Origins, IPs, Ranges, ASN's or Emails to be added, removed or set ); $result_array = \proxycheck\proxycheck::listing($proxycheck_options);
访问仪表板API时,始终需要API密钥,因为仪表板仅对注册用户开放(免费和付费用户均可完全访问仪表板)。您还需要确保在网站上启用了仪表板API访问权限(在您的仪表板中)。
您可以看到,在LIST_ENTRIES字段中,我们提供了一个包含三个单独条目的数组,此字段仅用于执行添加、删除或设置操作。当在LIST_ENTRIES字段中提供条目时,您可以包括注释,例如'LIST_ENTRIES' => array('8.8.8.8 #this is google')
将创建一个带有注释的新条目。
此LIST_ENTRIES字段也可以用于添加、设置或删除用于CORS功能的源。在这种情况下,不支持注释,仅支持域名(包括通配符域名,例如*.example.com)。所有LIST_ACTION类型都支持与CORS一样的方式与白名单/黑名单操作。
如果您要删除的白名单或黑名单条目旁边有注释,您需要在删除请求中也包括该注释,就像在上面的例子中,我们包括了添加IP地址时的注释,删除它时也应这样做。