You are here

function _vat_number_connect_vies_database in VAT Number 7

Try to connect with the VIES database. Catch PHP warnings if connection with the host is not possible.

1 call to _vat_number_connect_vies_database()
_vat_number_validate_vat in ./vat_number.inc
Check a VAT number. Return error message on error, FALSE on success.

File

./vat_number.inc, line 445
vat_number.inc

Code

function _vat_number_connect_vies_database() {
  if (!class_exists('SoapClient')) {
    drupal_set_message(t('The PHP SOAP extension is not installed on this server. The VAT ID could not be validated.'), 'error');
    return FALSE;
  }
  try {

    // Initialize the SoapClient options.
    $options = array(
      'exceptions' => TRUE,
    );

    // The VIES load balance servers require the User-Agent HTTP header, but PHP
    // versions 5.3 before 5.3.11 and version 5.4.0 contain a bug where the
    // SoapClient class ignores the user_agent option. In that case we must use
    // the PHP configuration to get it to send the User-Agent HTTP header.
    if (version_compare(PHP_VERSION, '5.3.11', '<') || version_compare(PHP_VERSION, '5.4.0', '=')) {

      // Set the PHP configuration option if it isn't already set.
      if (!ini_get('user_agent')) {
        ini_set('user_agent', 'PHP');
      }
    }
    else {

      // Set the SoapClient option, as the bug is not present.
      $options += array(
        'user_agent' => 'PHP',
      );
    }
    return @new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', $options);
  } catch (SoapFault $e) {

    // Connection to host not possible, europe.eu down?
    watchdog('vat_number', $e->faultstring, NULL, WATCHDOG_ERROR);
  }
}