The Core

Why We Are Here => Web Development => Topic started by: Chunkford on February 03, 2017, 04:15:07 PM

Title: Anyone a regex pro in here? Could do with a little help please.
Post by: Chunkford on February 03, 2017, 04:15:07 PM
I'm fighting with a web service that is returning a single message packed into a MIME MultiPart message (so I understand it).
So instead of just XML that PHP SOAP can process happily, I'm getting a SoapFault exception: [Client] looks like we got no XML document in.... because there's extra data before and after the XML.

This is what's being returned:

--uuid:80805a48-8715-46db-93f0-781f9ba28317+id=786
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
....
....
</s:Envelope>
--uuid:80805a48-8715-46db-93f0-781f9ba28317+id=786--


So, I'm planning on extending the SoapClient and overloading the __doRequest so I can manipulate the response and remove all the unwanted crap, for a better word.

class extSoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $response = parent::__doRequest($request, $location, $action, $version, $one_way);

        // strip away everything but the xml. <s:Envelope .... </s:Envelope>
       
        return $response;
    }
}


The only problem I have is working how to do it.
I'm sure I can use preg_replace() but I haven't a foggiest what regex to create to make the magic happen. It's definitely not my forte.
Can anyone help, please?
Title: Re: Anyone a regex pro in here? Could do with a little help please.
Post by: BoL on February 03, 2017, 08:28:52 PM
You'll probably want a try/catch wrapped around your request so the exception is caught and you can do the regex in the  catch section.

Quoterichard@richard-W65-W67RC:~$ php -a
Interactive mode enabled

php > $str = '<something else>
php ' <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
php ' <blah>
php ' </s:Envelope>
php ' <blah2>';
php >
php >
php > preg_match("'<s:Envelope.+</s:Envelope>'Uims",$str,$m);
php > print_r($m);
Array
(
   
  • => <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <blah>
    </s:Envelope>
    )
If it matches, $m[0] will have the s:Envelope element and its children.

The 'U' flag in the regex means ungreedy.. "keep trying to match until you find a match and no more"
Title: Re: Anyone a regex pro in here? Could do with a little help please.
Post by: Chunkford on February 04, 2017, 11:45:15 AM
Cheers buddy.
When I'm in front of the computer next I will give it a whirl.