2009-01-22 35 views
42

Bất kỳ ai cũng biết cách tôi có thể gửi Yêu cầu SOAP từ PHP?Cách đăng Yêu cầu SOAP từ PHP

+0

Đây là một ví dụ điển hình: http://stackoverflow.com/questions/7120586/soap-request-in-php – Iladarsda

+0

Các bạn đã nhìn vào 'tài liệu php.net'? Bắt đầu [tại đây] (http://php.net/manual/en/ref.soap.php) và chính xác hơn [tại đây] (http://php.net/manual/en/function.soap-soapclient-dorequest .php) Keltia

Trả lời

3

Bạn có thể muốn xem herehere.

Một mã nhỏ ví dụ từ các liên kết đầu tiên:

<?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); 
?> 
35

Theo kinh nghiệm của tôi, nó không hoàn toàn đơn giản. Việc xây dựng trong PHP SOAP client không hoạt động với máy chủ SOAP dựa trên .NET mà chúng tôi đã sử dụng. Nó phàn nàn về một định nghĩa lược đồ không hợp lệ. Mặc dù .NET client đã làm việc với máy chủ đó tốt. Nhân tiện, hãy để tôi cho rằng khả năng tương tác SOAP là một huyền thoại.

Bước tiếp theo là NuSOAP. Điều này làm việc khá lâu. Nhân tiện, vì Chúa, đừng quên cache WSDL! Nhưng ngay cả với những người dùng được lưu trữ trong WSDL đã phàn nàn điều tồi tệ là chậm.

Sau đó, chúng tôi quyết định đi HTTP trần, lắp ráp theo yêu cầu và đọc các câu trả lời với SimpleXMLElemnt, như thế này:

$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"; 
} 

Lưu ý rằng trong PHP 5.2 bạn sẽ cần pecl_http, như xa như (surprise- surpise!) không có ứng dụng HTTP nào được tích hợp sẵn.

Việc chuyển sang HTTP trống đã giúp chúng tôi vượt quá 30% thời gian yêu cầu SOAP. Và từ đó chúng tôi chuyển hướng tất cả các hiệu suất than phiền cho các chàng trai máy chủ.

Cuối cùng, tôi khuyên bạn nên sử dụng cách tiếp cận thứ hai này chứ không phải vì hiệu suất. Tôi nghĩ rằng, nói chung, trong một ngôn ngữ năng động như PHP có không có lợi ích từ tất cả các WSDL/loại kiểm soát. Bạn không cần một thư viện ưa thích để đọc và viết XML, với tất cả những gì tạo ra các proxy thế hệ và năng động. Ngôn ngữ của bạn đã hoạt động và SimpleXMLElement hoạt động tốt và rất dễ sử dụng. Ngoài ra, bạn sẽ có ít hơn .

24

PHP có hỗ trợ SOAP. Chỉ cần gọi

$client = new SoapClient($url); 

để kết nối với SoapServer và sau đó bạn có thể nhận được danh sách các hàm và chức năng cuộc gọi chỉ đơn giản bằng cách làm ...

$client->__getTypes();  
$client->__getFunctions(); 

$result = $client->functionName(); 

để biết thêm http://www.php.net/manual/en/soapclient.soapclient.php

+0

URL '$ url' có phải là tệp WSDL không? – HartleySan

7

tôi cần phải làm nhiều yêu cầu XML rất đơn giản và sau khi đọc nhận xét của @Ivan Krechetov về tốc độ nhấn của SOAP, tôi đã thử mã của anh ta và phát hiện http_post_data() không được xây dựng trong PHP 5.2. Không thực sự muốn cài đặt nó, tôi đã thử cURL trên tất cả các máy chủ của tôi. Mặc dù tôi không biết cURL nhanh như thế nào so với SOAP, nhưng chắc chắn là dễ dàng để làm những gì tôi cần. Dưới đây là một mẫu với cURL cho bất cứ ai cần nó.

$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); 
2

Dưới đây là ví dụ nhanh về cách thực hiện điều này (được giải thích tốt nhất về vấn đề này) mà tôi tìm thấy ở số this website. Liên kết trang web đó cũng giải thích WSDL, điều quan trọng để làm việc với các dịch vụ SOAP.

Tuy nhiên, tôi không nghĩ địa chỉ API mà họ đang sử dụng trong ví dụ bên dưới vẫn hoạt động, vì vậy, chỉ cần chuyển sang một trong những lựa chọn của riêng bạn.

$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); 
2

Chúng tôi có thể sử dụng thư viện PHP cURL để tạo yêu cầu HTTP POST đơn giản. Ví dụ sau đây cho bạn thấy cách tạo một yêu cầu SOAP đơn giản bằng cách sử dụng cURL.

Tạo soap-server.php viết yêu cầu SOAP vào soap-request.xml trong thư mục web.

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/ 
Các vấn đề liên quan