一个用户一直在帮我解决一个问题(
How to force a curl request in a PHP method to fail for a unit test
). 他们建议这样做
putenv('all_proxy=localhost:5678');
所以我可以强迫你
curl
在单元测试中动态失败(我改变了
http_proxy
/
https_proxy
到
all_proxy
因为它执行所有协议)。
任何一个
我的Windows 10机箱
或者Ubuntu盒
. 如果我设定
卷曲
php.ini
这就决定了
putenv()
是否可以覆盖环境中的变量?为什么动态环境变量可以工作
他们的设置,但不是我的
不是Windows而是Ubuntu?
测试脚本
<?php
function search() {
$url = 'x3m.dev';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
]);
$data = curl_exec($curl);
if (!$data) {
throw new Exception('An error occurred while trying to process the request.');
}
return $data;
}
function do_curl_request() {
echo getenv('all_proxy') . "\n\n";
try {
echo search();
}
catch (Exception $e) {
echo $e->getMessage();
}
echo "\n\n";
}
echo "========== first run without proxy\n";
do_curl_request();
putenv('all_proxy=localhost:5678');
echo "========== second run with proxy override\n";
do_curl_request();
所有代理
未设置为Windows环境变量,如果
一切就绪。
窗口(不正确)
========== first run without proxy
<html>
<head></head>
<body>.</body>
</html>
========== second run with proxy override
localhost:5678
<html>
<head></head>
<body>.</body>
</html>
Ubuntu(正确)
========== first run without proxy
<html>
<head></head>
<body>.</body>
</html>
========== second run with proxy override
localhost:5678
An error occurred while trying to process the request.