2012-01-10 4 views
7

잠시 동안 PHP 개발자 임에도 불구하고 이제 막 웹 서비스를 처음 접하게되었습니다. 내가 사용하고있는 책이별로 도움이되지 않기 때문에 약간의 도움을 얻고 싶었습니다. 우리가 사업을하고있는 회사 중 하나가 필요로하는 형식의 XML 문서를 내게주었습니다 (나는 그 덩어리를 게시 할 것입니다). 이 특정 주제에 대한 나의 미숙 함으로 인해, 나는 정말로 무엇을 해야할지 잘 모릅니다. 이 메시지를 실제 POST 페이지로 보내는 방법, 응답받는 방법을 알아야하며 WSDL 페이지를 작성해야합니까? 모든 도움이나 방향은 매우 높이 평가 될 것이며, PHP 매뉴얼에 대한 링크를 보내지 말아주십시오. 나는 분명히 거기에 있었는데, 일반적으로 도움을 요청할 장소이기 때문입니다.PHP SOAP HTTP 요청

POST /sample/order.asmx HTTP/1.1 
Host: orders.sample.com 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Header> 
    <AuthenticationHeader xmlns="http://sample/"> 
     <Username>string</Username> 
     <Password>string</Password> 
    </AuthenticationHeader> 
    <DebugHeader xmlns="http://sample/"> 
     <Debug>boolean</Debug> 
     <Request>string</Request> 
    </DebugHeader> 
    </soap12:Header> 
    <soap12:Body> 
    <AddOrder xmlns="http://sample/"> 
     <order> 
     <Header> 
      <ID>string</ID> 
      <EntryDate>dateTime</EntryDate> 
      <OrderEntryView> 
      <SeqID>int</SeqID> 
      <Description>string</Description> 
      </OrderEntryView> 
      <ReferenceNumber>string</ReferenceNumber> 
      <PONumber>string</PONumber> 
      <Comments>string</Comments> 
      <IpAddress>string</IpAddress> 
     </Header> 
     </order> 
    </AddOrder> 
    </soap12:Body> 
</soap12:Envelope> 

위의 내용은 내가 준 AddOrder XML 문서입니다 (본문의 대부분을 삭제했습니다). 가능한 한 구체적인 정보를 원한다면 자세한 정보가 필요하면 알려주십시오.이 정보를 보내는 방법을 파악할 수 있습니다.

답변

7

시작입니다! 비누 객체를 사용하여 요청을 작성하면 WSDL에 따라 원격 서버와 통신하는 올바른 방법을 알 수 있습니다. 이것을 수행하는 방법은 PHP manual에서 확인할 수 있습니다.

또는 CURL을 사용하여 작업을 수행 할 수 있습니다. 그런 다음 은에 결과를해야

$curlData = "<?xml version="1.0" encoding="utf-8"?>... etc"; 
$url='http://wherever.com/service/'; 
$curl = curl_init(); 

curl_setopt ($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl,CURLOPT_TIMEOUT,120); 
curl_setopt($curl,CURLOPT_ENCODING,'gzip'); 

curl_setopt($curl,CURLOPT_HTTPHEADER,array (
    'SOAPAction:""', 
    'Content-Type: text/xml; charset=utf-8', 
)); 

curl_setopt ($curl, CURLOPT_POST, 1); 
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData); 

$result = curl_exec($curl); 
curl_close ($curl); 

: (위의 예처럼 보이는)에 데이터를 게시 할 위치를 그런 다음 당신은 그냥 같은 것을 할 수 알아야합니다 $ result var. 그런 다음 인코딩으로 인해 가끔 발견되었지만 작동하지 않는 경우에도 XML 문서로 변환하려고 할 수 있습니다.

$xml = new SimpleXMLElement($result); 
print_r($xml); 
+0

여기 $ URL을 어떻게 알 수 있습니까? 감사. –

1

서비스 제공 업체는 다른 회사에서 WSDL 문서를 제공해야합니다. 이는 컴퓨터가 읽을 수있는 용어로 서비스를 설명합니다. 일반적으로 URL은 http://their.service.url/wsdl 또는 유사한 URL을 통해 제공됩니다.

일단 서비스를 사용하려면 SoapClient 인스턴스를 생성 할 수 있어야합니다.

1

이것은 확실히 SOAP 요청이므로 SOAP를 사용하여 올바르게 작동해야합니다. 그렇지 않으면 큰 골치 거리가 있습니다.

저는 SOAP과 PHP에 대한 몇 가지 만남을 가지고 있으며 매번 외부 라이브러리에 의존해야했습니다. 가장 최근에 사용한 것은 Zend_Soap_Client입니다.

하지만 다시 WSDL을 사용할 수 있습니까? 클라이언트 라이브러리를 사용하여 SOAP 웹 서비스를 사용할 수 있으려면 WSDL이 필요합니다.

http://framework.zend.com/manual/en/zend.soap.html

그리고 여기 내가 사용하는 내 코드, 내가 그것을 얻을 것이다 희망의 샘플은 당신이 당신은 몇 가지 옵션이

ini_set('soap.wsdl_cache_enabled', 0); 
set_include_path(dirname(__FILE__).'/../../libraries/:'.get_include_path()); 
require_once 'Zend/Loader/Autoloader.php'; 
Zend_Loader_Autoloader::getInstance(); 

//Include the classes for the webservice 
include('CatalogOrder.php'); 
include('CatalogOrderItem.php'); 
include('CatalogOrderWebservice.php'); 

//Check the mode 
if(isset($_GET['wsdl'])) { 

    $autodiscover = new Zend_Soap_AutoDiscover(array(
     'classmap' => array(
      'CatalogOrder' => "CatalogOrder", 
      'CatalogOrderItem' => "CatalogOrderItem" 
     ) 
    )); 
    $autodiscover->setComplexTypeStrategy(new Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex()); 
    $autodiscover->setClass('CatalogOrderWebService'); 
    $autodiscover->handle(); 

//Return the consume form and process the actions of the consumer 
} elseif(isset($_GET['consume'])) { 

    // pointing to the current file here 
    $soap = new Zend_Soap_Client("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
     'classmap' => array(
      'CatalogOrder' => "CatalogOrder", 
      'CatalogOrderItem' => "CatalogOrderItem" 
     ), 
     'encoding' => 'iso-8859-1' 
    )); 
    include('CatalogOrderWebserviceConsumer.php'); 

//Process SOAP requests 
} else { 

    // pointing to the current file here 
    $soap = new Zend_Soap_Server("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
     'classmap' => array(
      'CatalogOrder' => "CatalogOrder", 
      'CatalogOrderItem' => "CatalogOrderItem" 
     ), 
     'encoding' => 'iso-8859-1' 
    )); 
    $soap->setClass('CatalogOrderWebService'); 
    $soap->handle(); 

}