2009-06-17 2 views

답변

0

Papa Google은 Soap (특히 PHP5의 구현)을 사용하여 클라이언트 및 서버 측면에서 많은 좋은 예가 나와이 점을 알려줍니다. 좋은 출발점처럼 보입니다.

WSDL을 직접 작성하는 생각에 다소 만족 스럽다면 PHP 리플렉션 클래스를 사용하여 WSDL을 동적으로 생성하는 WSHelper을 사용하는 것이 좋습니다. 확실히 시간을 절약하십시오

1

기본적으로 클래스 맵을 만들어 비누 클라이언트에 전달해야합니다. 예, 그것은 고통입니다. 나는 보통 Soap Object 이름을 PHP 객체 (즉, Person => MY_Person)에 매핑하는 메소드를 가지고 있으며, 필요로하는 것들만 코드화합니다 (예 : createdOn => DateTime).

class MY_WSHelper 
{ 
    protected static $ws_map; 
    public static function make_map() 
    { 
    if(! self::$ws_map) 
    { 
     self::$ws_map = array(); 
     //These will be mapped dynamically 
     self::$ws_map['Person'] = NULL; 
     self::$ws_map['Animal'] = NULL; 
     //Hard-coded type map 
     self::$ws_map['createdOn'] = DateTime; 
     self::$ws_map['modifiedOn'] = DateTime; 

     foreach(self::$ws_map as $soap_name => $php_name) 
     { 
     if($php_name === NULL) 
     { 
      //Map un-mapped SoapObjects to PHP classes 
      self::$ws_map[$soap_name] = "MY_" . ucfirst($soap_name); 
     } 
     } 
    } 
    return self::$ws_map; 
    } 
} 

클라이언트 :

$client = new SoapClient('http://someurl.com/personservice?wsdl', 
    array('classmap' => MY_WSHelper::make_map())); 

$aperson = $client->getPerson(array('name' => 'Bob')); 
echo get_class($aperson); //MY_Person 
echo get_class($aperson->createdOn); //DateTime 

http://php.net/manual/en/soapclient.soapclient.php

2

내가 Zend_Soap_Server를 사용하고는 Zend_Soap_Client을 и. 어려운 구조의 배열을 보내거나받습니다.

먼저 수신하려는 구조로 클래스를 생성하십시오. 이제 WSDL을 (얻을

<?php 

//disable wsdl caching 
ini_set('soap.wsdl_cache_enabled', 0); 
ini_set('soap.wsdl_cache', 0); 

$wsdl = $_GET['wsdl']; 

//this generate wsdl from our class SoapService 
if (!is_null($wsdl)) 
{ 
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence'); 
    $autodiscover->setClass('SoapService'); 
    $autodiscover->handle(); 
} 
//handle all soap request 
else 
{ 
    $wsdlPath = 'http://ourhost/soap/?wsdl'; 

    $soap = new Zend_Soap_Server($wsdlPath, array(
     'cache_wsdl' => false 
    )); 
    $soap->registerFaultException('Zend_Soap_Server_Exception'); 
    $soap->setClass('SoapService'); 
    $soap->handle(); 
} 

?> 

을 및 HTTP :

<?php 

/** 
* Service to receive SOAP data 
*/ 
class SoapService 
{ 
    /** 
    * 
    * @param PeopleInformation $data 
    * @return string 
    */ 
    public function getUserData($data) 
    { 
     //here $data is object of PeopleInformation class 


     return "OK"; 
    } 
} 
?> 
이제

URL http://ourhost/soap/에 의해 컨트롤러 Zend_Soap_Server 인스턴스를 만들 : // ourhost

<?php 

/** 
* Information about people 
*/ 
class PeopleInformation 
{ 
    /** 
    * Name of ... 
    * 
    * @var string 
    */ 
    public $name; 

    /** 
    * Age of 
    * @var int 
    */ 
    public $age; 

    /** 
    * Array of family 
    * 
    * @var FamilyInformation[] 
    */ 
    public $family; 
} 

/** 
* Information about his family 
*/ 
class FamilyInformation 
{ 
    /** 
    * Mother/sister/bro etc 
    * 
    * @var string 
    */ 
    public $relation; 

    /** 
    * Name 
    * @var string 
    */ 
    public $name; 
} 

?> 

는이 데이터를 수신하는 서비스를 만들/soap /? wsdl)을 사용하여 SoapService :: getUserData에 요청을 구조화하고 처리하십시오. 이 메소드의 입력 매개 변수는 의 객체입니다. PeopleInformation 클래스

0

저의 (나쁜) 경험을 나누기 위해 다시 재생합니다.

PHP ZendFramework2 (ZF2)를 사용하여 웹 서비스를 만들었습니다.

서버는 객체와 객체의 배열을 응답하고, 문자열을 입력으로 받아 들일 때까지 제대로 작동합니다. ArrayOfTypeComplex 전략을 사용하고있었습니다.

$_strategy = new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeComplex(); 

내가 라밀의 답을 찾아 낼 때까지 나는 어둡고 불행 계곡에 느낌 입력으로 문자열의 배열을 사용하려고, 그래서 전략을 변경하고 모든 잘 작동 !

$_strategy = new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence(); 

if (isset($_GET['wsdl'])) { 
    $autodiscover = new \Zend\Soap\AutoDiscover($_strategy); 
    $autodiscover->setBindingStyle(array('style' => 'document')); 
    $autodiscover->setOperationBodyStyle(array('use' => 'literal')); 
    $autodiscover->setClass('Tracker\Queue\Service') 
     ->setUri($_serverUrl); 
    echo $autodiscover->toXml(); 
} 
관련 문제