Author Topic: Anyone a regex pro in here? Could do with a little help please.  (Read 1492 times)

Chunkford

  • Inner Core
  • Hero Member
  • *
  • Posts: 1034
    • View Profile
Anyone a regex pro in here? Could do with a little help please.
« 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:

Code: [Select]
--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.

Code: [Select]
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?
"If my answers frighten you then you should cease asking scary questions"

BoL

  • Inner Core
  • Hero Member
  • *
  • Posts: 896
    • View Profile
Re: Anyone a regex pro in here? Could do with a little help please.
« Reply #1 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.

Quote
richard@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
(
   
<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"

Chunkford

  • Inner Core
  • Hero Member
  • *
  • Posts: 1034
    • View Profile
Re: Anyone a regex pro in here? Could do with a little help please.
« Reply #2 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.
"If my answers frighten you then you should cease asking scary questions"