PHP代码样例
发布时间:2024-04-23
$url = 'https://要访问的链接';
// 创建一个 cURL 句柄
$ch = curl_init();
$type = 'http'; //要是用的协议 根据实际修改
$ip = ''; //通过接口提取出来的ip
$port = ''; //通过接口提取出来的端口
$text = "{$type}";
switch ($type){
case 'http':
// 代理服务器地址和端口
$proxy = "$ip:$port";
break;
case 'ss5':
// 代理服务器地址和端口
$proxy = "socks5://$ip:$port";
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
}
// 设置代理服务器
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// 设置需要请求的 URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置一些其他选项,例如忽略 SSL 验证和超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// 将结果保存到变量中,而不是输出到浏览器
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行 cURL 请求并获取响应
$response = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
// 获取连接时间(即从建立连接到开始传输数据所用的时间)
$connectTime = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
// 获取开始传输数据的时间
$startTransferTime = curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME);
// 获取总耗时
$timeTaken = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
// 输出耗时信息
$text .= "
花费的总时间: $timeTaken 秒";
$text .= "
连接时间: $connectTime 秒";
$text .= "
开始传输时间: $startTransferTime 秒";
}
// 关闭 cURL 句柄
curl_close($ch);