PHP 爬虫代理示例
代码示例说明
- 代码样例不能直接运行,请替换成您自己的代理信息。
- 在不同编程语言的代码示例中,需注意其环境版本。
- 示例代码使用遇到问题请,我们会为您提供技术支持。
1<?php2// 要访问的目标页面3$url = "http://httpbin.org/ip";4$urls = "https://httpbin.org/ip";56// 代理服务器(产品官网 www.16yun.cn)7define("PROXY_SERVER", "tcp://t.16yun.cn:31111");89// 代理身份信息10define("PROXY_USER", "username");11define("PROXY_PASS", "password");1213$proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);1415// 设置 Proxy tunnel16$tunnel = rand(1,10000);1718$headers = implode("\r\n", [19"Proxy-Authorization: Basic {$proxyAuth}",20"Proxy-Tunnel: ${tunnel}",21]);22$sniServer = parse_url($urls, PHP_URL_HOST);23$options = [24"http" => [25"proxy" => PROXY_SERVER,26"header" => $headers,27"method" => "GET",28'request_fulluri' => true,29],30'ssl' => array(31'SNI_enabled' => true, // Disable SNI for https over http proxies32'SNI_server_name' => $sniServer33)34];35print($url);36$context = stream_context_create($options);37$result = file_get_contents($url, false, $context);38var_dump($result);3940// 访问 HTTPS 页面41print($urls);42$context = stream_context_create($options);43$result = file_get_contents($urls, false, $context);44var_dump($result);45?>