PHP请求库Guzzle配置代理

2024-12-03 106 0

    public function run() {
//        $response = $this->httpClient->request('GET', 'https://plugin.newmorehot.com/v2/index/ip', [
//            'proxy' => '代理IP:代理端口',
//            'curl' => [
//                CURLOPT_PROXYUSERPWD => '用户名:密码'
//            ]
//        ]);
        $response = $this->httpClient->request('GET', 'https://plugin.newmorehot.com/v2/index/ip', [
            'proxy' => '127.0.0.1:10809',
            'curl' => [
                CURLOPT_PROXYUSERPWD => 'user:123456'
            ]
        ]);
        $code = $response->getStatusCode(); // 200
        $reason = $response->getReasonPhrase(); // OK
//        print_r(array(
//            'code'      => $code,
//            'reason'    => $reason
//        ));
        if ($code != 200 && $reason != 'OK') {
            return false;
        }
        $bodyContents = $response->getBody()->getContents();
        print_r($bodyContents);
    }

curl原生案例

$url = 'http://dynupdate.no-ip.com/ip.php';
$proxy = '127.0.0.1:8888';
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

参考资料

    相关文章

    织音云上新香港Platinum 8358处理器性能怪兽,爆炸高配折后最低仅需32元/月,数据对标物理机
    2025年华纳云新年焕新季,香港云4H4G3M特惠696元/年,E5物理服务器688元/月起
    莱卡云:2025年1月香港云服务器、美国云服务器、日本云服务器促销活动
    php使用ip-api根据ip地扯获取位置信息
    PHP简单实现随机一言
    万维电讯:香港VPS推荐 4H4G3M CN2 GIA线路三网直连 仅需$9.9/月

    发布评论