如何在 php 脚本中为不同的 services/programs 使用代理

How to use proxies for different services/programs in php script

我有这个测试php脚本:

<?php

$proxy = "62.111.14.138:80";
$proxy2 = "104.111.208.238:80";

$_SERVER["http_proxy"] = "http://name:pass@".$proxy;
$_SERVER["https_proxy"] = "http://name:pass@".$proxy;

print_r($_SERVER["http_proxy"]);
echo "\n";
print_r($_SERVER["https_proxy"]);
echo "\n";

$string = ('./speedtest-cli');
  $descriptorspec = array(
      0 => array("pipe", "r"),  // stdin
      1 => array("pipe", "w"),  // stdout
      2 => array("pipe", "w"),  // stderr
      );
  $process = proc_open($string, $descriptorspec, $pipes);
  $stdout = stream_get_contents($pipes[1]);
  fclose($pipes[1]);
  $stderr = stream_get_contents($pipes[2]);
  fclose($pipes[2]);
  $ret = proc_close($process);
  echo json_encode(array('status' => $ret, 'errors' => $stderr,
  'output' => $stdout,
  'command' => $string));
echo "\n";

$_SERVER["http_proxy"] = "http://name:pass@".$proxy2;
$_SERVER["https_proxy"] = "http://name:pass@".$proxy2;

print_r($_SERVER["http_proxy"]);
echo "\n";
print_r($_SERVER["https_proxy"]);
echo "\n";

$string = ('./speedtest-cli');
  $descriptorspec = array(
      0 => array("pipe", "r"),  // stdin
      1 => array("pipe", "w"),  // stdout
      2 => array("pipe", "w"),  // stderr
      );
  $process = proc_open($string, $descriptorspec, $pipes);
  $stdout = stream_get_contents($pipes[1]);
  fclose($pipes[1]);
  $stderr = stream_get_contents($pipes[2]);
  fclose($pipes[2]);
  $ret = proc_close($process);
  echo json_encode(array('status' => $ret, 'errors' => $stderr,
  'output' => $stdout,
  'command' => $string));
  echo "\n";

?>

输出:

http://name:pass@62.111.14.138:80
http://name:pass@62.111.14.138:80
{"status":0,"errors":"","output":"Retrieving speedtest.net configuration...\nRetrieving speedtest.net server list...\nTesting from OVH SAS (92.111.195.34)...\nSelecting best server based on latency...\nHosted by FreeMobile (Paris) [1.30 km]: 8.026 ms\nTesting download speed........................................\nDownload: 204.45 Mbit\/s\nTesting upload speed..................................................\nUpload: 18.70 Mbit\/s\n","command":".\/speedtest-cli"}
http://name:pass@104.111.208.238:80
http://name:pass@104.111.208.238:80
{"status":0,"errors":"","output":"Retrieving speedtest.net configuration...\nRetrieving speedtest.net server list...\nTesting from OVH SAS (92.111.195.34)...\nSelecting best server based on latency...\nHosted by FreeMobile (Paris) [1.30 km]: 8.494 ms\nTesting download speed........................................\nDownload: 162.78 Mbit\/s\nTesting upload speed..................................................\nUpload: 18.94 Mbit\/s\n","command":".\/speedtest-cli"}

看起来它忽略了代理,只使用服务器的 ip 92.111.195.34

如何让它使用指定的代理而不是服务器的 ip?

我正在使用:

CentOS Linux 发行版 7.1.1503(核心)

PHP 5.4.16 (cli)(内置:2014 年 10 月 31 日 12:59:36)

非常感谢Arnau Sanchez aka tokland

这是解决方案

http_proxy=xyz https_proxy=xyz ./speedtest-cli

所以我们可以像这样使用它:

$http_proxy = "http://name:pass@1.1.1.1:80";
$https_proxy = "http://name:pass@1.1.1.1:80";

$string = ('http_proxy='.$http_proxy.' https_proxy='.$https_proxy.' ./speedtest-cli');
  $descriptorspec = array(
      0 => array("pipe", "r"),  // stdin
      1 => array("pipe", "w"),  // stdout
      2 => array("pipe", "w"),  // stderr
      );
  $process = proc_open($string, $descriptorspec, $pipes);
  $stdout = stream_get_contents($pipes[1]);
  fclose($pipes[1]);
  $stderr = stream_get_contents($pipes[2]);
  fclose($pipes[2]);
  $ret = proc_close($process);
  echo json_encode(array('status' => $ret, 'errors' => $stderr,
  'output' => $stdout,
  'command' => $string));