如何在PHP脚本中使用不同服务/程序的代理

时间:2015-06-16 05:29:12

标签: php linux proxy centos

我有这个测试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)

1 个答案:

答案 0 :(得分:0)

非常感谢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));