2016-04-04 15 views
3

Bir \ SoapClient yanıtını XML'den bir dosyadan almak istiyorum.Sahte XML'den SoapClient yanıtı

SoapClient bir dosyadan döndüğü gibi stdClass nesnesini nasıl oluşturabilirim?

İstemci zaten SoapClient'i sarar, böylece kolayca yanıt alay edebilirsiniz.

$soapClient->expects($this->once()) 
      ->method('call') 
      ->will(
       $this->returnValue(
        simplexml_load_string(
         file_get_contents(__DIR__ . '/../../../Resources/file.xml') 
        ) 
       ) 
      ); 

Ama bu SimpleXML olup stdClass verir:

Benim sahte böyledir.

Güncelleme:
önerilen json_encode/json_decode hack doesnt SoapClient onları döndüren gibi nitelikleri işlemek görünmektedir:

SoapClient:

object(stdClass)[4571] 
    public 'Lang' => string 'de' (length=2) 
    public 'Success' => boolean true 

Hack:

object(stdClass)#27 (3) { 
    ["@attributes"]=> 
    object(stdClass)#30 (2) { 
    ["Lang"]=> 
    string(2) "de" 
    ["Success"]=> 
    string(4) "true" 
+0

bu deneyin: '$ my_std_class = json_decode (json_encode ($ my_simplexmlelement)); doesnt – Matteo

cevap

2

You json aşağıdaki gibi SimpleXml kodlayabilir/çözebilir:

$soapClient->expects($this->once()) 
     ->method('call') 
     ->will(
      $this->returnValue(
       json_decode(json_encode(
        simplexml_load_string(
         file_get_contents(__DIR__ . '/../../../Resources/file.xml') 
        ) 
       )) 
      ) 
     ); 

Ama konserve cevabını bir php sınıfı olarak açıkça tanımlamanızı öneririm.

+0

elinde gibi görünüyor' SoapClients aynı nitelikleri cevap ben bunu test edilmiş ve sonuçlar – ivoba

+0

Ah eklendi, görüyorum. Sunucu yanıtını beklenen sonuç değil, xml dosyasına koyarsınız. Bu durumda ['SoapClient :: __ doRequest'] (http://php.net/manual/en/soapclient.dorequest.php) yöntemini kullanmanız daha iyi olur. –

+0

evet bu işe yarayabilir. ya da döndürülen nesneyi bir dosyaya serileştirir ve php nesnesini, soapClient :: __ doRequest ipucu için – ivoba