• PHP使用嵌入HTTP代理代码示例


    以下是使用 PHP 嵌入 HTTP 代理的示例代码:

    ```php

    // 设置代理服务器地址和端口

    $proxy = '127.0.0.1:8080';

    // 设置代理服务器用户名和密码(如果需要验证)

    $proxyAuth = 'username:password';

    // 创建 cURL 句柄

    $ch = curl_init();

    // 设置 cURL 选项

    curl_setopt($ch, CURLOPT_URL, 'Example Domain');

    curl_setopt($ch, CURLOPT_PROXY, $proxy);

    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // 执行 cURL 请求

    $response = curl_exec($ch);

    // 检查是否有错误发生

    if(curl_errno($ch)) {

    echo 'cURL Error: ' . curl_error($ch);

    }

    // 关闭 cURL 句柄

    curl_close($ch);

    // 输出响应内容

    echo $response;

    ?>

    ```

    在上面的示例中,我们使用 `curl_setopt()` 函数设置了 cURL 的选项,包括代理服务器地址和端口、代理服务器用户名和密码(如果需要验证)以及返回响应内容。最后,我们使用 `curl_exec()` 函数执行 cURL 请求,并使用 `curl_errno()` 函数检查是否有错误发生。如果没有错误发生,我们就可以使用 `curl_close()` 函数关闭 cURL 句柄,并输出响应内容。

    1. // 要访问的目标页面
    2. $targetUrl = "http://ip.hahado.cn/ip";
    3. //$targetUrl = "http://ip.hahado.cn/switch-ip";
    4. //$targetUrl = "http://ip.hahado.cn/current-ip";
    5. // 代理服务器
    6. define("PROXY_SERVER", "ip.hahado.cn:39010");
    7. // 隧道身份信息
    8. define("PROXY_USER", "username");
    9. define("PROXY_PASS", "password");
    10. $proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);
    11. $headers = implode("\r\n", [
    12. "Proxy-Authorization: Basic {$proxyAuth}",
    13. "Proxy-Switch-Ip: yes",
    14. ]);
    15. $options = [
    16. "http" => [
    17. "proxy" => $proxyServer,
    18. "header" => $headers,
    19. "method" => "GET",
    20. ],
    21. ];
    22. $context = stream_context_create($options);
    23. $result = file_get_contents($url, false, $context);
    24. var_dump($result);
  • 相关阅读:
    从 Java 小白到收获 BAT 一线大厂offer ,分享出我这 2年的经验
    2017年,我成为了技术博主
    Java面试题整理Java基础部分
    zabbix的rpm包部署
    改行后我在做什么?(2022-9-19日晚)
    mpu6050移植DMP库
    Java 正则表达式
    Idea debug失败,但是run可以正常
    tuend\stratis\vdo总结和课堂案例
    当线性规划与算法相遇:揭秘单纯形法(Simplex)的独特魅力
  • 原文地址:https://blog.csdn.net/weixin_73725158/article/details/130848613