我认为让 stream_context_create() 与 socks5 一起工作是不可能的。最好的解决方案是使用 curl。示例根据你的需要,更改$proxyIP和$proxyPort
<?php
//$opt = array('socks5' => array(
// 'proxy' => 'tcp://proxyIP:port',
// 'request_fulluri' => true,
// )
// );
//
//$stream = stream_context_create($opt);
//
//if ($s = file_get_contents("http://yandex.ru/internet",FILE_USE_INCLUDE_PATH,$stream))
//echo $s;
$url = 'https://yandex.ru/internet';
$proxyIP = '0.0.0.0';
$proxyPort = 0;
//$proxy_user = '';
//$proxy_pass = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxy_user}:{$proxy_pass}");
curl_setopt($ch, CURLOPT_PROXY, $proxyIP);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
$s = curl_exec($ch);
curl_close($ch);
if ($s) {
echo $s;
}
如果您不能使用 curl 或想使用套接字,请阅读此https://stackoverflow.com/a/31010287/3904683 答案