任何人都知道如何从PHP发布SOAP请求?
根据我的经验,它并不那么简单.内置的PHP SOAP客户端无法使用我们必须使用的基于.NET的SOAP服务器.它抱怨架构定义无效.即使.NET客户端使用该服务器就好了.顺便说一下,让我声称SOAP互操作性是一个神话.
下一步是NuSOAP.这工作了很长一段时间.顺便说一句,为了上帝的缘故,不要忘记缓存WSDL!但即使WSDL缓存用户抱怨该死的东西很慢.
然后,我们决定使用HTTP,组合请求并阅读响应SimpleXMLElemnt
,如下所示:
$request_info = array(); $full_response = @http_post_data( 'http://example.com/OTA_WS.asmx', $REQUEST_BODY, array( 'headers' => array( 'Content-Type' => 'text/xml; charset=UTF-8', 'SOAPAction' => 'HotelAvail', ), 'timeout' => 60, ), $request_info ); $response_xml = new SimpleXMLElement(strstr($full_response, 'xpath('//@HotelName') as $HotelName) { echo strval($HotelName) . "\n"; }
请注意,在PHP 5.2中,你需要pecl_http,至于(惊讶 - 惊讶!),没有内置的HTTP客户端.
在SOAP请求时间内,使用HTTP使我们获得了超过30%的收益.从那时起,我们将所有性能抱怨重定向到服务器人员.
最后,我建议使用后一种方法,而不是因为性能.我认为,一般来说,在像PHP这样的动态语言中,所有WSDL /类型控制都没有任何好处.您不需要花哨的库来读取和写入XML,以及所有存根生成和动态代理.您的语言已经是动态的,并且SimpleXMLElement
工作正常,并且易于使用.此外,您将拥有更少的代码,这总是很好.
PHP具有SOAP支持.打电话吧
$client = new SoapClient($url);
连接到SoapServer然后你可以通过简单的方式得到函数列表和调用函数...
$client->__getTypes(); $client->__getFunctions(); $result = $client->functionName();
更多http://www.php.net/manual/en/soapclient.soapclient.php
我需要做很多非常简单的XML请求,在阅读了@Ivan Krechetov关于SOAP速度命中的评论之后,我尝试了他的代码并发现http_post_data()没有内置到PHP 5.2中.不是真的想安装它,我尝试了所有服务器上的cURL.虽然我不知道cURL与SOAP相比有多快,但确实很容易做到我需要的东西.以下是需要cURL的示例.
$xml_data = ''; $URL = "https://test.testserver.com/PriceAvailability"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); print_r($output); 123 abc 99999 1
我们可以使用PHP cURL库生成简单的HTTP POST请求。以下示例显示了如何使用cURL创建简单的SOAP请求。
创建soap-server.php,它将SOAP请求写入Web文件夹中的soap-request.xml中。
We can use the PHP cURL library to generate simple HTTP POST request. The following example shows you how to create a simple SOAP request using cURL.
Create the soap-server.php which write the SOAP request into soap-request.xml in web folder.
The next step is creating the soap-client.php which generate the SOAP request using the cURL library and send it to the soap-server.php URL.
\n";
$soap_request .= "\n";
$soap_request .= " \n";
$soap_request .= " \n";
$soap_request .= " IBM \n";
$soap_request .= " \n";
$soap_request .= " \n";
$soap_request .= " ";
$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://localhost/php-soap-curl/soap-server.php" );
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
curl_close($soap_do);
print 'Operation completed without any errors';
}
?>
Enter the soap-client.php URL in browser to send the SOAP message. If success, Operation completed without any errors will be shown and the soap-request.xml will be created.
IBM
Original - http://eureka.ykyuen.info/2011/05/05/php-send-a-soap-request-by-curl/