How to prevent a method from printing data when calling a third-party API?

10

I am using an API and in some cases a feature of it prints on the screen when there are errors (api errors, not PHP). It happens that I call this API via AJAX and it breaks my code since the request ends up generating an invalid JSON.

Is there any way to avoid this in PHP? For example, from a certain passage of the code to another passage, nothing is printed on the screen?

Something like this:

// desativaria impressão de qualquer coisa aqui
$this->soapClient = new SoapClient($wsdlLink);
$retorno = $this->soapClient->testFunction($params);
// ativaria impressão aqui

In the above case when I call testFunction, instead of saving the error in the $ return variable, it prints on the screen.

When you access a SOAP resource you are calling a function pre-established by the SOAP API developer. It turns out that sometimes testFunction prints the error directly and not as callback.

What can I do to resolve this problem?

    
asked by anonymous 23.01.2014 / 19:36

3 answers

12

You can use PHP's Output Buffer to capture output without sending to the client :

ob_start(); 
$this->soapClient = new SoapClient($wsdlLink);
$retorno = $this->soapClient->testFunction($params);
$output_retido = ob_get_contents(); //opcional, caso queira usar o que foi retido
ob_end_clean();
    
23.01.2014 / 19:53
4

It has a way to inhibit the impression of errors in the core of PHP, which is to use error control operator @ before the command:

$retorno = @$this->soapClient->testFunction($params);
    
23.01.2014 / 19:47
3

By using display_errors it is possible to hide / display errors in time of execution in a specific script.

ini_set('display_errors', false);
$this->soapClient = new SoapClient($wsdlLink);
$retorno = $this->soapClient->testFunction($params);
ini_set('display_errors', true);
    
23.01.2014 / 19:43