php - 在第三方库中记录 SOAP 信封

标签 php soap envelope

我正在尝试为第三方库生成的信封添加日志记录。我正在修改下面的 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 类上公开该方法,因为 $client 是私有(private)变量。正确的调用方式是$this->client->__getLastRequest()

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

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

关于php - 在第三方库中记录 SOAP 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343019/

相关文章:

php - Foreach 在 where 语句中?

PHP SOAP 和命名空间。 PHP fatal error : Class 'Jira\SoapClient' not found

android - 使用 Ksoap2 检查信封 (Android)

c++ - 尝试在 OpenSSL 中使用 EVP 函数时出现段错误

matlab - 通过在 MATLAB 中获取上下包络来封装信号

php - 移动 woocommerce 小部件价格过滤器的 slider handle 起始位置

php - 从 PHP 打印到 POS 打印机

php - 在php mysql中加入4个表

c# - 从 WSDL (.NET) 生成 SOAP 请求

c# - 无法从 VS2017 中的 WSDL 生成代理