3

I am attempting to add logging for the envelope generated by a third party library. I am modifying the updateMetadataField() method below.

I am creating $client like so:

$client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);

I have tried both $this->client->__getLastRequest() and $this->__getLastRequest() with the same error as a result.

When the SoapClient is instantiated trace is set to true.

Error is

Fatal error:  Call to undefined method UpdateClient::__getLastRequest() 

So how do I correctly access the __getLastRequest() method?

$USER_AUTH_ARRAY = array(
    'login'=>"foo",
    'password'=>"bar",
    'exceptions'=>0,
    'trace'=>true,
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);

class UpdateClient {
    private $client;

    public function __construct($endpoint, $auth_array) {
        $this->client = new SoapClient($endpoint, $auth_array);
    }

    public function updateMetadataField($uuid, $key, $value) {
        $result = $this->client->updateMetadataField(array(
            'assetUuid' =>  $uuid, 
            'key' =>        $key,
            'value' =>      $value)
        );

        if(is_soap_fault($result)) {
            return $result;
        }

        return $result->return . "\n\n" . $this->client->__getLastRequest();

    } // updateMetadataField()

} // UpdateClient

UPDATE - adding calling code This code iterates over an array which maps our data to the remote fields.

What I am hoping to do is begin storing the envelope we send to aid in debugging.

    $client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);
    foreach ($widen_to_nool_meta_map as $widen => $nool) { // array defined in widen.php
        if ($nool != '') {
            // handle exceptions
            if ($nool == 'asset_created') { // validate as date - note that Widen pulls exif data so we don't need to pass this
                if (!strtotime($sa->$nool)) {
                    continue;   
                }
            } else if ($nool == 'people_in_photo' || $nool == 'allow_sublicensing' || $nool == 'allowed_use_pr_gallery') {  
                // we store as 0/1 but GUI at Widen wants Yes/No
                $sa->$nool = ($sa->$nool == '1') ? 'Yes' : 'No';
            } else if ($nool == 'credit_requirements') {
                $sa->$nool = $sa->credit_requirements()->label;
            }

            $result = $client->updateMetadataField($sa->widen_id, $widen, $sa->$nool);
            if(is_soap_fault($result)) {    
                $sync_result    = $sync_result . "\n" . $result->getMessage();
            } else {
                $sync_result    = $sync_result . "\n" . print_r($result, 1);
            }
        } // nool field set         
    } // foreach mapped field
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
  • The code works fine and such error would appear if you are calling `$client->__getLastRequest();` (from outside the class) or `$this->__getLastRequest()` in the return statement. Can you share more of the code - including the piece where you call `updateMetadataField`? – VolenD Sep 05 '15 at 19:25
  • Added code used to call updateMetadataField(). But are you saying in comment that the error would be expected if called in the function return? – jerrygarciuh Sep 06 '15 at 21:16
  • If `$this->__getLastRequest();` is in the return statement, then that error will be raised. If you use `$this->client->__getLastRequest();` however there will be no problems. Here is a simplified example which you can test: http://codepad.viper-7.com/5oJSS0 – VolenD Sep 06 '15 at 22:10
  • Problem continues when `$this->client->__getLastRequest()` is in return and even when its output is captured in var and appended to return... `$envelope = $this->client->__getLastRequest(); return $result->return . "\n\n$envelope";` – jerrygarciuh Sep 07 '15 at 16:22

1 Answers1

2

If you want to access UpdateClient::__getLastRequest() you have to expose that method on the UpdateClient class since the $client is a private variable. The correct way of calling it is $this->client->__getLastRequest().

Take a look at this working example, as you can see I'm consuming a free web service for testing purposes.

<?php
$USER_AUTH_ARRAY = array(
    'exceptions'=>0,
    'trace'=>true,
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);

class TestClient {
    private $client;

    public function __construct($endpoint, $auth_array) {
        $this->client = new SoapClient($endpoint, $auth_array);
    }

    public function CelsiusToFahrenheit( $celsius ) {
        $result = $this->client->CelsiusToFahrenheit(array(
            'Celsius' =>  $celsius
            )
        );

        if(is_soap_fault($result)) {
            return $result;
        }

        return $result;

    }

    public function __getLastRequest() {
        return $this->client->__getLastRequest();
    }

} 

try 
{
    $test = new TestClient( "http://www.w3schools.com/webservices/tempconvert.asmx?wsdl", $USER_AUTH_ARRAY);
    echo "<pre>";
    var_dump($test->CelsiusToFahrenheit( 0 ));
    var_dump($test->__getLastRequest());
    var_dump($test->CelsiusToFahrenheit( 20 ));
    var_dump($test->__getLastRequest());
    echo "</pre>";
} 
catch (SoapFault $fault) 
{ 
    echo $fault->faultcode;
}
?>
rdgfuentes
  • 346
  • 1
  • 8
  • Thank you! So I should be able to append the output of __getLastRequest() to the return inside of CelsiusToFahrenheit() correct? – jerrygarciuh Sep 08 '15 at 13:54
  • Yes, you should be able to do that. Remember that the SoapClient::__getLastRequest() method returns an XML string and will not be visible in the browser, but you can still see it in the html source or use ``htmlentities()`` to see it in the html view. – rdgfuentes Sep 08 '15 at 14:00
  • I'm logging the output (when in debug mode) to the database. It works fine now from the calling script but even when I appended the XML to the return string it threw the error. This does not matter since I can log from the calling script now. Thank you! – jerrygarciuh Sep 08 '15 at 14:47
  • Sorry- I had assumed choosing yours as the answer awarded the bounty. It has been awarded now. Appreciate your help! – jerrygarciuh Sep 08 '15 at 21:59