How to parse in the return of the class SoapClient method __getFunctions ()

2

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

I get 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 the 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
  • QueryType
asked by anonymous 17.11.2016 / 13:37

3 answers

1

With a well-structured ER, you accomplish this in just a few lines.

First of all, it's important to understand that until the method nomenclature follows a regular expression :

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

From this, you can extend the regular expression to search whenever there is a method name, compatible with the regular expression above, between a blank 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 name of the method, it is important to include in a group (which I will name method):

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

And the code to 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']);
}

Output:

Método: Consultar 
Método: ConsultarTipo 
    
17.11.2016 / 17:08
0

For your submitted template, your query would look 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 this, it would basically be 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 in IDEONE

    
17.11.2016 / 15:05
0

You can use the strpos() function to find out the position of the first space and open it, so you can determine which part should be 'trimmed' with the substr() function, ie it starts after the space ( $espaço+1 ) and copy 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>';
}   
    
17.11.2016 / 14:43