Soap 客户端在服务器上运行;在 CLI 上不连接,而 curl 在 CLI 上连接
Soap Client works on server; does not connect on CLI, while curl does connect on CLI
简而言之,我有一个简单的 SoapClient 脚本:
- 在我的开发机器上工作 (win - IIS)
- 使用 PHPUnit 时无法连接到我的本地计算机
奇怪的是,在我的 PHPUnit 测试中,curl_init()
和 curl_exec()
连接而 SoapClient
不连接。
IIS 上的工作脚本:
<?php
$response = "No message received";
$wsdl = 'http://www.dneonline.com/calculator.asmx?WSDL';
$trace = true;
$exceptions = false;
$xml_array['intA'] = 5;
$xml_array['intB'] = 3;
try {
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->Add($xml_array);
$foo = $client->Subtract($xml_array);
} catch (Exception $e) {
echo "Error!";
echo $e -> getMessage ();
echo 'Last response: '. $client->__getLastResponse();
}
print "<pre>";
print_r($client->__getFunctions());
var_dump($response);
if($response->AddResult == 8) {
print "Yep, it's eight";
}
print "\n-----------------------\n";
var_dump($foo);
if($foo->SubtractResult == 2) {
print "Yep, it's two";
}
PHPUnit 测试:
public function testTruth() {
$this->assertTrue(true);
}
public function testCurl() {
$url = 'http://www.dneonline.com/calculator.asmx?WSDL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$output = curl_exec($curl);
$this->assertEquals('<?xml', substr($output,0,5));
}
public function testSoapClient() {
$response = "No message received";
$wsdl = 'http://www.dneonline.com/calculator.asmx?WSDL';
$options = array(
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true,
'stream_context' => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient')))
);
libxml_disable_entity_loader(false);
$client = new SoapClient($wsdl,$options); // test dies here...
PHP单元输出:
PHPUnit 6.0.10 by Sebastian Bergmann and contributors.
..PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.dneonline.com/calculator.asmx?WSDL' : failed to load external entity "http://www.dneonline.com/calculator.asmx?WSDL"
in X:\websites\development\tests\soapTest.php on line 48
ER 4 / 4 (100%)
Time: 1.65 seconds, Memory: 10.00MB
There was 1 error:
1) RDBTest::testSoapClient
SoapFault: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.dneonline.com/calculator.asmx?WSDL' : failed to load external entity "http://www.dneonline.com/calculator.asmx?WSDL"
我从其他 SO 建议中尝试过的内容:
- 开发服务器和本地 CLI 相比 php.ini。添加了本地 CLI 中不存在的来自服务器的任何指令。
- 已添加
libxml_disable_entity_loader(false)
- 已添加
'cache' => WSDL_CACHE_NONE
- 已添加
'stream_context => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient')))
知道为什么 new SoapClient
无法连接,而 curl
正在连接吗?
代理...问题是我的本地计算机在代理之后。修复最终变得如此简单:
$options = array(
//'trace' => true,
//'cache' => WSDL_CACHE_NONE,
//'exceptions' => false,
//'stream_context' => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient'))),
'proxy_host' => '{host name}',
'proxy_port' => '{port}',
'proxy_login' => '{username}',
'proxy_password' => '{password}'
);
//libxml_disable_entity_loader(false);
$client = new SoapClient($wsdl,$options);
五个小时的谷歌搜索和搜索...
答案隐藏在 PHP 文档中:/
简而言之,我有一个简单的 SoapClient 脚本:
- 在我的开发机器上工作 (win - IIS)
- 使用 PHPUnit 时无法连接到我的本地计算机
奇怪的是,在我的 PHPUnit 测试中,curl_init()
和 curl_exec()
连接而 SoapClient
不连接。
IIS 上的工作脚本:
<?php
$response = "No message received";
$wsdl = 'http://www.dneonline.com/calculator.asmx?WSDL';
$trace = true;
$exceptions = false;
$xml_array['intA'] = 5;
$xml_array['intB'] = 3;
try {
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->Add($xml_array);
$foo = $client->Subtract($xml_array);
} catch (Exception $e) {
echo "Error!";
echo $e -> getMessage ();
echo 'Last response: '. $client->__getLastResponse();
}
print "<pre>";
print_r($client->__getFunctions());
var_dump($response);
if($response->AddResult == 8) {
print "Yep, it's eight";
}
print "\n-----------------------\n";
var_dump($foo);
if($foo->SubtractResult == 2) {
print "Yep, it's two";
}
PHPUnit 测试:
public function testTruth() {
$this->assertTrue(true);
}
public function testCurl() {
$url = 'http://www.dneonline.com/calculator.asmx?WSDL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$output = curl_exec($curl);
$this->assertEquals('<?xml', substr($output,0,5));
}
public function testSoapClient() {
$response = "No message received";
$wsdl = 'http://www.dneonline.com/calculator.asmx?WSDL';
$options = array(
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true,
'stream_context' => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient')))
);
libxml_disable_entity_loader(false);
$client = new SoapClient($wsdl,$options); // test dies here...
PHP单元输出:
PHPUnit 6.0.10 by Sebastian Bergmann and contributors.
..PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.dneonline.com/calculator.asmx?WSDL' : failed to load external entity "http://www.dneonline.com/calculator.asmx?WSDL"
in X:\websites\development\tests\soapTest.php on line 48
ER 4 / 4 (100%)
Time: 1.65 seconds, Memory: 10.00MB
There was 1 error:
1) RDBTest::testSoapClient
SoapFault: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.dneonline.com/calculator.asmx?WSDL' : failed to load external entity "http://www.dneonline.com/calculator.asmx?WSDL"
我从其他 SO 建议中尝试过的内容:
- 开发服务器和本地 CLI 相比 php.ini。添加了本地 CLI 中不存在的来自服务器的任何指令。
- 已添加
libxml_disable_entity_loader(false)
- 已添加
'cache' => WSDL_CACHE_NONE
- 已添加
'stream_context => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient')))
知道为什么 new SoapClient
无法连接,而 curl
正在连接吗?
代理...问题是我的本地计算机在代理之后。修复最终变得如此简单:
$options = array(
//'trace' => true,
//'cache' => WSDL_CACHE_NONE,
//'exceptions' => false,
//'stream_context' => stream_context_create(array('http'=>array('user_agent'=>'PHPSoapClient'))),
'proxy_host' => '{host name}',
'proxy_port' => '{port}',
'proxy_login' => '{username}',
'proxy_password' => '{password}'
);
//libxml_disable_entity_loader(false);
$client = new SoapClient($wsdl,$options);
五个小时的谷歌搜索和搜索...
答案隐藏在 PHP 文档中:/