在第三方库中记录 SOAP 信封

Logging SOAP envelope in third party library

我正在尝试为第三方库生成的信封添加日志记录。我正在修改下面的 updateMetadataField() 方法。

我正在这样创建 $client:

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

我试过 $this->client->__getLastRequest()$this->__getLastRequest() 都出现了同样的错误。

实例化 SoapClient 时 trace 设置为 true

错误是

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

那么如何正确访问__getLastRequest()方法呢?

$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

更新 - 添加调用代码此代码遍历一个将我们的数据映射到远程字段的数组。

我希望做的是开始存储我们发送来帮助调试的信封。

    $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

如果您想访问 UpdateClient::__getLastRequest(),您必须在 UpdateClient class 上公开该方法,因为 $client 是私有变量。正确的调用方式是$this->client->__getLastRequest().

看看这个工作示例,您可以看到我正在使用免费的 Web 服务进行测试。

<?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;
}
?>