在 PHP 中使用 SOCKS5 代理,您可以选择使用 cURL 或者 Guzzle 这样的 HTTP 客户端库
<?php $url = 'http://example.com'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_PROXY, 'socks5://proxy.example.com:1080'); // 将此行替换为您的 SOCKS5 代理地址和端口 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); $response = curl_exec($ch); if ($response === false) { echo 'Error: ' . curl_error($ch); } else { echo $response; } curl_close($ch); ?> 首先,确保已安装 Guzzle 客户端库。如果尚未安装,请运行以下命令进行安装:
composer require guzzlehttp/guzzle 然后,使用以下代码配置 SOCKS5 代理:
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $url = 'http://example.com'; $proxy = 'socks5://proxy.example.com:1080'; // 将此行替换为您的 SOCKS5 代理地址和端口 $client = new Client([ 'base_uri' => $url, 'timeout' => 5.0, ]); try { $response = $client->request('GET', '/', [ 'proxy' => $proxy, ]); echo $response->getBody(); } catch (\GuzzleHttp\Exception\RequestException $e) { echo 'Error: ' . $e->getMessage(); } ?> 这些示例展示了如何在 PHP 中使用 cURL 和 Guzzle 配置 SOCKS5 代理。根据您的需求和喜好选择一个库进行实现。