sveawebpay / php-integration

SDK for Sveas payment methods (standalone and Svea Checkout)
Other
15 stars 19 forks source link

[Feature Request] Raw HTTP log #71

Closed timint closed 6 years ago

timint commented 7 years ago

For debug and support matters it's proven to (many times over) be helpful if we could access some sort of raw http log of the Request and Response:

E.g.

$swp->getLastHTTPDebug();

## [2017-01-17 19:51:56] Raw HTTP Request ##########

POST /Folder/Resource HTTP/1.1
Host: api.fancydomain.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.6.29
Content-Type: text/xml; charset=utf-8
Content-Length: 592

<?xml version="1.0" encoding="UTF-8"?>
<Foo/>

## [2017-01-17 19:51:56] Raw HTTP Response — 8,218 bytes transferred in 0.657 s ##########

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 8000
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 17 Jan 2017 18:51:55 GMT

<?xml version="1.0"?>
<Bar/>

I would easier get an immediate understanding of what might have gone wrong during communication.

timint commented 7 years ago

+1 more case today where this had become handy

timint commented 7 years ago

An example of how I use to log raw HTTP data from the SOAPClient

class SveaSOAPClient extends SOAPClient {
    private $_request = array();
    private $_response = array();

    public function __construct($wsdl, $options=array()) {

      $options['trace'] = true;
      $options['exceptions'] = true;

      parent::__construct($wsdl, $options);
    }

    public function __doRequest($request, $location, $action, $version, $one_way = 0) {

      $timestamp_start = time();      
      $microtime_start = microtime(true);

      try {
        $result = parent::__doRequest($request, $location, $action, $version, $one_way);
      } catch (SoapFault $fault) {
        throw new Exception($fault->getMessage());
      }

      // PHP Bug - parent::__getLastResponse() returned null, however print_r($result, true) returned xml content
      $response_body = print_r($result, true);

      $this->_http_request = array( 
        'timestamp' => $timestamp_start,
        'head' => parent::__getLastRequestHeaders(),
        'body' => functions::xml_pretty_print(parent::__getLastRequest()),
      );

      $this->_http_response = array(
        'timestamp' => time(),
        'head' => parent::__getLastResponseHeaders() . PHP_EOL,
        'body' => functions::xml_pretty_print($response_body),
        'data_amount' => strlen(parent::__getLastResponseHeaders()) + strlen($response_body),
        'duration' => round(microtime(true) - $microtime_start, 3),
      );

      return $result;
    }
  }
timint commented 7 years ago

+1 for a case earlier this week

timint commented 7 years ago

+1 for another client case today

fre-sund commented 6 years ago

Added in 3.6.0, soap requests now contains logs if the user decides to enable them