如何从PHP发布SOAP请求
从第一链接一些代码,例如:
<?php
// include the SOAP classes
require_once('nusoap.php');
// define parameter array (ISBN number)
$param = array('isbn'=>'0385503954');
// define path to server application
$serverpath ='http://services.xmethods.net:80/soap/servlet/rpcrouter';
//define method namespace
$namespace="urn:xmethods-BNPriceCheck";
// create client object
$client = new soapclient($serverpath);
// make the call
$price = $client->call('getPrice',$param,$namespace);
// if a fault occurred, output error info
if (isset($fault)) {
print "Error: ". $fault;
}
else if ($price == -1) {
print "The book is not in the database.";
} else {
// otherwise output the result
print "The price of book number ". $param[isbn] ." is $". $price;
}
// kill object
unset($client);
?>
根据我的经验,这不是那么简单。内置的PHP SOAP client不适用于我们必须使用的基于.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, '<?xml'));
foreach ($response_xml->xpath('//@HotelName') as $HotelName) {
echo strval($HotelName) . "\n";
}
注意,在PHP 5.2你需要pecl_http,只要(surprise- surpise!)有没有内置的HTTP客户端。
要裸HTTP获得了美国对在SOAP请求倍30%。从那时起,我们将所有的性能投诉重定向到服务器人员。
最后,我建议后一种方法,并没有因为性能。我认为,一般来说,在像PHP这样的动态语言中,对所有WSDL /类型控制都没有好处。您不需要一个花哨的库来读取和写入XML,包含所有的存根和动态代理。你的语言已经是动态的了,SimpleXMLElement
工作得很好,而且使用起来非常简单。此外,你将有减少代码,这总是很好。
PHP有SOAP支持。 只需拨打
$client = new SoapClient($url);
连接到SoapServer的,然后你可以简单地做得到的功能和通话功能列表...
$client->__getTypes();
$client->__getFunctions();
$result = $client->functionName();
我需要做的许多非常简单的XML请求,在阅读@Ivan Krechetov关于SOAP速度命中的评论之后,我尝试了他的代码,发现http_post_data()没有内置到PHP 5.2中。不是真的想要安装它,我尝试了所有服务器上的cURL。虽然我不知道cURL与SOAP相比有多快,但确实很容易做到我所需要的。以下是cURL的示例,供需要它的任何人使用。
$xml_data = '<?xml version="1.0" encoding="UTF-8" ?>
<priceRequest><customerNo>123</customerNo><password>abc</password><skuList><SKU>99999</SKU><lineNumber>1</lineNumber></skuList></priceRequest>';
$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);
下面是如何做到这一点(其最好的解释这件事对我来说)一个简单的例子,我基本上是在this website发现。该网站链接还解释了WSDL,这对于使用SOAP服务非常重要。
不过,我不认为他们使用在下面的例子中仍然有效的API地址,所以只是在你自己选择的一个开关。
$wsdl = 'http://terraservice.net/TerraService.asmx?WSDL';
$trace = true;
$exceptions = false;
$xml_array['placeName'] = 'Pomona';
$xml_array['MaxItems'] = 3;
$xml_array['imagePresence'] = true;
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->GetPlaceList($xml_array);
var_dump($response);
我们可以使用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.
<?php
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$f = fopen("./soap-request.xml", "w");
fwrite($f, $HTTP_RAW_POST_DATA);
fclose($f);
?>
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.
<?php
$soap_request = "<?xml version=\"1.0\"?>\n";
$soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n";
$soap_request .= " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n";
$soap_request .= " <m:GetStockPrice>\n";
$soap_request .= " <m:StockName>IBM</m:StockName>\n";
$soap_request .= " </m:GetStockPrice>\n";
$soap_request .= " </soap:Body>\n";
$soap_request .= "</soap:Envelope>";
$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.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
Original - http://eureka.ykyuen.info/2011/05/05/php-send-a-soap-request-by-curl/
这里是一个很好的例子:http://*.com/questions/7120586/soap-request-in-php – Iladarsda 2011-08-24 08:22:11
有你看看`php.net`文档?开始[这里](http://php.net/manual/en/ref.soap.php),更确切地说
[here](http://php.net/manual/en/function.soap-soapclient-dorequest .php)– Keltia 2009-01-22 22:36:00