How to parse the return of the __getFunctions() class Soapclient method

Asked

Viewed 137 times

2

I consulted a Web Service using the class SoapClient of PHP and the method __getFunctions() to list the services provided by this Web Service.

I got the return below:

Array
(
    [0] => ConsultarResponse Consultar(ConsultarRequest $ConsultarRequest)
    [1] => ConsultarTipoResponse, ObjResponse ConsultarTipo(ConsultarTipoRequest $ConsultarTipoRequest)
)

How do I parse to get the word that comes before relatives? The idea would be to ignore everything and take only the occurrence that comes to the left of the relatives.

The return would have to be:

  • Constular
  • Consultartipo
  • The word Consultar ?

  • in the first would be Consult and in the second line Consultartipo. Always catch the first sentence that comes before relatives. For example: Query Answer Query Type(Query $Query Query) would be to pick only Query Type.

3 answers

1

With a well structured ER, you do it in a few lines.

First of all, it is important to understand that until the nomenclature of methods follows a regular expression:

'^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$'

From this, you can expand the regular expression to search whenever there is a method name, compatible with the above regular expression, between a blank space and the opening of a parenthesis:

'\s[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\(/'

As you want to extract only the method name, it is important to include in a group (which I nomarei of method):

'\s(?P<method>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\(/'

And the code for use:

$stringList = [
    'ConsultarResponse Consultar(ConsultarRequest $ConsultarRequest)',
    'ConsultarTipoResponse, ObjResponse ConsultarTipo(ConsultarTipoRequest $ConsultarTipoRequest)'
];

$matches = [];

foreach($stringList as $string) {   
    preg_match(
        '/\s(?P<method>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\(/',
        $string,
        $matches
    );

    printf("Método: %s <br />" , $matches['method']);
}

Exit:

Método: Consultar 
Método: ConsultarTipo 

0

You can use the function strpos() to find the position of the first space and of the open prenhesis, with this it is possible to determine which section should be 'cropped' with the function substr() that is to begin at after the occurrence of space ($espaço+1) and copies N characters this done with $parentese - $espaco -1.

$arr = array('ConsultarResponse Consultar(ConsultarRequest $ConsultarRequest)',
             'ConsultarTipoResponse ConsultarTipo(ConsultarTipoRequest $ConsultarTipoRequest)'
            );

foreach($arr as $item){
    $espaco = strpos($item, ' ');
    $parentese = strpos($item, '(');
    echo substr($item, $espaco+1, $parentese - $espaco -1) .'<br>';
}

You can use a regex with a group ($m[1]) to capture exactly what is between the space (\s+) and parenthesis.

foreach($arr as $item){
    preg_match('/\s+(\w+)\(/', $item, $m);
    echo $m[1] .'<br>';
}   
  • There may be more than one space before the method. For example: Query Answer, Objrsponse Query (Query $Query Query). Then it will be necessary to take only the occurrence before the parentheses. I think it would be something like, has a space and is preceded by parentheses?

  • @Fabio edited the answer.

  • a Regex helped a lot, however, still need to be returned the occurrence to the left of the parentheses, final bounder parentheses and initial bounder last space before the parentheses.

0

By the model presented, your query would be something like this:

$parametro = 'Sua consulta';

$client = new SoapClient('http://path/seuwebservice.wsdl');
$client->ConsultarResponse->Consultar($parametro);
$client->ConsultarTipoResponse->ConsultarTipo($parametro);

To make a parse of it, it would be basically like this:

$return  = $client->__getFunctions();

list($use, $notUse) = explode('(', $return[0]);
list($ConsultarResponse, $Consultar) = explode(' ',$use);
//primeiro
echo $Consultar;

list($useTwo, $notUseTwo) = explode('(',$return[1]);
list($ConsultarTipoResponse, $ConsultarTipo) = explode(', ObjResponse ', $useTwo);
//segundo
echo $ConsultarTipo;

See the example on IDEONE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.