任何人都知道如何從PHP發佈SOAP請求?如何從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/
- 1. 從PHP發送SOAP請求
- 2. 使用php發佈SOAP請求
- 3. 如何打印soap請求(請求使用php curl發送)?
- 4. SOAP請求PHP
- 5. PHP - SOAP請求
- 6. PHP SOAP請求
- 7. 如何從瀏覽器發佈SOAP請求?
- 8. PHP發佈請求
- 9. 如何從SOAP請求
- 10. 如何像SOAP UI請求一樣從android構建SOAP請求?
- 11. 發送SOAP請求
- 12. 發出php soap請求的問題
- 13. 通過PHP向CRM2011發送SOAP請求
- 14. Php蛋糕發佈請求
- 15. PHP SOAP如何創建嵌套請求
- 16. PHP/SOAP:UserCredentials如何傳遞給SOAP請求?
- 17. 如何使用soapui發送SOAP請求
- 18. 你如何發送SOAP請求?
- 19. 如何發送curl中的SOAP請求
- 20. 如何在android中發送soap請求?
- 21. 如何發送2個XML請求soap?
- 22. 如何通過POSTMAN發送SOAP請求
- 23. 如何在Android中發佈SOAP服務請求?
- 24. 如何在WSO2 API Manager中使用Swagger發佈SOAP請求?
- 25. 我可以使用SOAP的HTTP發佈請求嗎? - SOAP和Django
- 26. 從php到CRMOneline的SOAP請求
- 27. 發送SOAP請求,從哪裏開始?
- 28. 如何從SOAP XML API請求答案?
- 29. 使用PHP的SOAP請求
- 30. 使用PHP的SOAP請求
這裏是一個很好的例子:http://stackoverflow.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