php
  • xml
  • curl
  • soap
  • 2016-03-29 16 views 0 likes 
    0

    私はPHPでカールを使用してSOAPリクエストを送信していますが、私は応答をのvar_dumpと私はPHPでcurlを使ってSOAPリクエストからXMLレスポンスを読み込んで解析するには?

    $soapUrl = "https://hosting/Wsdl"; // asmx URL of WSDL 
    
    $xml_post_string = '<?xml version="1.0" encoding="utf-8"?> 
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    
            <soapenv:Header/> 
             <soapenv:Body> 
             <MY DATA> 
             </soapenv:Body> 
            </soapenv:Envelope>'; 
    
    $headers = array(
        "Host: hosting, 
        "Connection: Keep-Alive", 
        "Accept: text/xml", 
        "Cache-Control: no-cache", 
        "Pragma: no-cache", 
        "Content-type: text/xml;charset=\"utf-8\"", 
        "SOAPAction: http://hosting.com/SOAPAction", 
        "Content-length: ".strlen($xml_post_string), 
    ); 
    $url = $soapUrl; 
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    
    // converting 
    $response = curl_exec($ch); 
    curl_close($ch); 
    // converting 
    var_dump($response); 
    
    
    $response1 = str_replace("<soap:Body>","",$response); 
    $response2 = str_replace("</soap:Body>","",$response1); 
    
    // convertingc to XML 
    $parser = simplexml_load_string($response); 
    
    echo '<pre>'; 
    var_dump($parser); 
    echo '</pre>'; 
    

    string(9965) "RESPONSE DATA" 
    
    object(SimpleXMLElement)#164 (0) { } 
    
    として返すレスポンスをXMLにそれを解析することができない場合の応答が文字列として返します

    ご協力いただき、ありがとうございます。

    答えて

    1

    私は、次のXMLParser

    <? 
    
    class XMLParser { 
    
        // raw xml 
        private $rawXML; 
        // xml parser 
        private $parser = null; 
        // array returned by the xml parser 
        private $valueArray = array(); 
        private $keyArray = array(); 
    
        // arrays for dealing with duplicate keys 
        private $duplicateKeys = array(); 
    
        // return data 
        private $output = array(); 
        private $status; 
    
        public function XMLParser($xml){ 
         $this->rawXML = $xml; 
         $this->parser = xml_parser_create(); 
         return $this->parse(); 
        } 
    
        private function parse(){ 
    
         $parser = $this->parser; 
    
         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); // Dont mess with my cAsE sEtTings 
         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);  // Dont bother with empty info 
         if(!xml_parse_into_struct($parser, $this->rawXML, $this->valueArray, $this->keyArray)){ 
          $this->status = 'error: '.xml_error_string(xml_get_error_code($parser)).' at line '.xml_get_current_line_number($parser); 
          return false; 
         } 
         xml_parser_free($parser); 
    
         $this->findDuplicateKeys(); 
    
         // tmp array used for stacking 
         $stack = array();   
         $increment = 0; 
    
         foreach($this->valueArray as $val) { 
          if($val['type'] == "open") { 
           //if array key is duplicate then send in increment 
           if(array_key_exists($val['tag'], $this->duplicateKeys)){ 
            array_push($stack, $this->duplicateKeys[$val['tag']]); 
            $this->duplicateKeys[$val['tag']]++; 
           } 
           else{ 
            // else send in tag 
            array_push($stack, $val['tag']); 
           } 
          } elseif($val['type'] == "close") { 
           array_pop($stack); 
           // reset the increment if they tag does not exists in the stack 
           if(array_key_exists($val['tag'], $stack)){ 
            $this->duplicateKeys[$val['tag']] = 0; 
           } 
          } elseif($val['type'] == "complete") { 
           //if array key is duplicate then send in increment 
           if(array_key_exists($val['tag'], $this->duplicateKeys)){ 
            array_push($stack, $this->duplicateKeys[$val['tag']]); 
            $this->duplicateKeys[$val['tag']]++; 
           } 
           else{     
            // else send in tag 
            array_push($stack, $val['tag']); 
           } 
           $this->setArrayValue($this->output, $stack, $val['value']); 
           array_pop($stack); 
          } 
          $increment++; 
         } 
    
         $this->status = 'success: xml was parsed'; 
         return true; 
    
        } 
    
        private function findDuplicateKeys(){ 
    
         for($i=0;$i < count($this->valueArray); $i++) { 
          // duplicate keys are when two complete tags are side by side 
          if($this->valueArray[$i]['type'] == "complete"){ 
           if($i+1 < count($this->valueArray)){ 
            if($this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag'] && $this->valueArray[$i+1]['type'] == "complete"){ 
             $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0; 
            } 
           } 
          } 
          // also when a close tag is before an open tag and the tags are the same 
          if($this->valueArray[$i]['type'] == "close"){ 
           if($i+1 < count($this->valueArray)){ 
            if( $this->valueArray[$i+1]['type'] == "open" && $this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag']) 
             $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0; 
           } 
          } 
    
         } 
    
        } 
    
        private function setArrayValue(&$array, $stack, $value){ 
         if ($stack) { 
          $key = array_shift($stack); 
          $this->setArrayValue($array[$key], $stack, $value); 
          return $array; 
         } else { 
          $array = $value; 
         } 
        } 
    
        public function getOutput(){ 
         return $this->output; 
        } 
    
        public function getStatus(){ 
         return $this->status;  
        } 
    
    } 
    
    ?> 
    
    Usage: 
    
    $p = new XMLParser($xml); 
    $p->getOutput(); 
    
    +0

    こんにちはが、そのは、タグ名を返さないを使用します。あなたはtagnamesを得る方法を知らせることができますか? – androidBeckhamania

    関連する問題