array("pipe", "w"), // stdout 2 => a-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:10 24 4
gpt4 key购买 nike

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

关于php - 如何在 php 脚本中为不同的服务/程序使用代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com