Error consuming webservice, content type “application/xop+xml” does not match expected type “text/xml”

前端 未结 7 1850
我在风中等你
我在风中等你 2020-11-30 03:16

I\'m having a weird issue when consuming a webservice for a product that my company has bought. The product is called Campaign Commander and it\'s made by a company called

7条回答
  •  既然无缘
    2020-11-30 03:50

    I got exactly the same problem with the same function and the same company and I spend a couple of hours on Google trying to find the right answer. After so many trials I finally got it. Here is what I did: The following string was the response that functions from Email Vision where returning me (My main purpose was Mass Upload but I swap to getLastUpload for simplicity and testing).

    string(2180) "

    --uuid:5c8a8a1d-a29c-43d0-baaa-cb3a8c189962
    Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
    Content-Transfer-Encoding: binary
    Content-ID: 
    
    254816API_BATCH_MEMBERDONE254810API_BATCH_MEMBERDONE254805API_BATCH_MEMBERDONE254799API_BATCH_MEMBERDONE254797API_BATCH_MEMBERDONE254791API_BATCH_MEMBERDONE254790API_BATCH_MEMBERDONE254771API_BATCH_MEMBERDONE254770API_BATCH_MEMBERERROR254759API_BATCH_MEMBERDONE254747API_BATCH_MEMBERDONE253619CCMDDONE238053CCMDDONE WITH ERROR(S)216618CCMDDONE WITH ERROR(S)200373CCMDDONE200367CCMDDONE195445CCMDDONE194579CCMDDONE194263CCMDDONE193740CCMDDONE
    --uuid:5c8a8a1d-a29c-43d0-baaa-cb3a8c189962--
    

    If you look at the top of the string it has a key value and other staff from the server which made the SoapClient class to throw an exception "looks like we got no XML document". Eventhough the xml document was below all this staff.

    --uuid:5c8a8a1d-a29c-43d0-baaa-cb3a8c189962
    Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
    Content-Transfer-Encoding: binary
    Content-ID: 
    

    Similarly at the bottom of the string (after the xml) the same key appears

    --uuid:5c8a8a1d-a29c-43d0-baaa-cb3a8c189962--
    

    So the solution was to try to get rid of the part of the string from the beginning to the start of the xml file and from the end of the xml file to the end of the string. I found a very good script that helped me to approach this solution here MTOM php client. However, my response xml was a bit different so I just modified that script a little bit and here is my version.

    function upload_file_insert_via_soap_obj( $token, $csv_file )
    {
        try
        {
            $wsdl_loc = 'https:XXX/apibatchmember/services/BatchMemberService?wsdl';
    
            $soap_Object = new MTOMSoapClient( $wsdl_loc, array( 'cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true ) );
    
            $parameters['token'] = $token;
            $parameters['insertUpload']['fileName'] = "insertMemberTest.txt";
            $parameters['insertUpload']['fileEncoding'] = "UTF-8";
            $parameters['insertUpload']['separator'] = ",";
            $parameters['insertUpload']['skipFirstLine'] = "false";
            $parameters['insertUpload']['autoMapping'] = "true"; 
    
            $parameters['file'] = file_get_contents( "insertMemberTest.txt" );
    
            $results = $soap_Object->uploadFileInsert( $parameters );
    
            $upload_id = $results->return;
    
            echo "
    upload_id: ".$upload_id; return $upload_id; } catch ( Exception $exception ) { echo "EX REQUEST: " . $soap_Object->__getLastRequest() . "
    "; echo "EX RESPONSE: " . $soap_Object->__getLastResponse() . "
    "; echo "
    Response var dump: "; var_dump( $soap_Object->__getLastResponse() ); echo "
    "; echo '

    Exception: '.$exception->getMessage()."
    "; var_dump( $exception ); } } /** * This client extends the ususal SoapClient to handle mtom encoding. Due * to mtom encoding soap body has test apart from valid xml. This extension * remove the text and just keeps the response xml. */ class MTOMSoapClient extends SoapClient { public function __doRequest( $request, $location, $action, $version, $one_way = 0 ) { $response = parent::__doRequest( $request, $location, $action, $version, $one_way ); //if resposnse content type is mtom strip away everything but the xml. if ( strpos( $response, "Content-Type: application/xop+xml" ) !== false ) { //not using stristr function twice because not supported in php 5.2 as shown below //$response = stristr(stristr($response, "", true) . ""; $tempstr = stristr( $response, "" ) ) . ""; } //log_message($response); return $response; } }

提交回复
热议问题