登录 注册

资讯文档中心

使用教程 API文档 SDK示例 IP资讯
如果有任何问题,请联系我们的客服,会有专人为您服务解答。希望九零科技的产品服务能带给您安全便利!

PHP配置代理请求——示例代码

$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 .= "开始传输时间: $startTransferTime 秒";
    $text .= "连接时间: $connectTime 秒";
    $text .= "花费的总时间: $timeTaken  秒";
}
// 关闭 cURL 句柄
curl_close($ch);
上一篇:九零住宅代理IP特点介绍 下一篇:python配置代理请求——示例代码