在PHP中使用SoapClient调用远程函数

时间:2015-11-12 05:38:53

标签: php soap wsdl soap-client

我使用SoapClient访问Pubmatic的远程功能:getPublisherAdTagReport() wsdl是:“http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService?wsdl

代码如下:

    $soapClient = new SoapClient("http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService?wsdl"); 

    // Prepare SoapHeader parameters 
    $head_param = array('Authorization'=>"Bearer ".$access_token); 
    $headers = new SoapHeader('http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService?wsdl', 'UserCredentials', $head_param); 

    // Prepare Soap Client 
    $soapClient->__setSoapHeaders(array($headers)); 

    try {
        $info = $soapClient->getPublisherAdTagReport(new SoapParam($access_token,"accessToken"),new SoapParam($pubId,"publisherId"),new SoapParam("2015-10-01","fromDate"),new SoapParam("2015-10-30","toDate")); 
        print_r($info);
    } catch (SoapFault $fault) { 
        print_r($fault); 
    } 

异常中返回的SoapFault对象如下:

    SoapFault Object
    (
        [message:protected] => Apigee Internal Error
        [string:Exception:private] => 
        [code:protected] => 0
        [file:protected] => /home/utkarsh/algoscale/soapTest.php
        [line:protected] => 62
        [trace:Exception:private] => Array
            (
                [0] => Array
                    (
                        [function] => __doRequest
                        [class] => SoapClient
                        [type] => ->
                        [args] => Array
                            (
                                [0] => <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.reporting.core.pubmatic.com/" xmlns:ns2="http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService?wsdl"><SOAP-ENV:Header><ns2:UserCredentials><item><key>Authorization</key><value>Bearer #######AccessToken######</value></item></ns2:UserCredentials></SOAP-ENV:Header><SOAP-ENV:Body><ns1:getPublisherAdTagReport/><publisherId>####3</publisherId><fromDate>2015-10-01</fromDate><toDate>2015-10-30</toDate></SOAP-ENV:Body></SOAP-ENV:Envelope>

                                [1] => http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService
                                [2] => 
                                [3] => 1
                                [4] => 0
                            )

                    )

                [1] => Array
                    (
                        [file] => /home/utkarsh/algoscale/soapTest.php
                        [line] => 62
                        [function] => __call
                        [class] => SoapClient
                        [type] => ->
                        [args] => Array
                            (
                                [0] => getPublisherAdTagReport
                                [1] => Array
                                    (
                                        [0] => SoapParam Object
                                            (
                                                [param_name] => accessToken
                                                [param_data] => #######AccessToken######
                                            )

                                        [1] => SoapParam Object
                                            (
                                                [param_name] => publisherId
                                                [param_data] => ####3
                                            )

                                        [2] => SoapParam Object
                                            (
                                                [param_name] => fromDate
                                                [param_data] => 2015-10-01
                                            )

                                        [3] => SoapParam Object
                                            (
                                                [param_name] => toDate
                                                [param_data] => 2015-10-30
                                            )

                                    )

                            )

                    )

                [2] => Array
                    (
                        [file] => /home/utkarsh/algoscale/soapTest.php
                        [line] => 62
                        [function] => getPublisherAdTagReport
                        [class] => SoapClient
                        [type] => ->
                        [args] => Array
                            (
                                [0] => SoapParam Object
                                    (
                                        [param_name] => accessToken
                                        [param_data] => #######AccessToken######
                                    )

                                [1] => SoapParam Object
                                    (
                                        [param_name] => publisherId
                                        [param_data] => ####3
                                    )

                                [2] => SoapParam Object
                                    (
                                        [param_name] => fromDate
                                        [param_data] => 2015-10-01
                                    )

                                [3] => SoapParam Object
                                    (
                                        [param_name] => toDate
                                        [param_data] => 2015-10-30
                                    )

                            )

                    )

            )

        [previous:Exception:private] => 
        [faultstring] => Apigee Internal Error
        [faultcode] => HTTP
    )

以下是我使用的结构:$ soapClient-&gt; __ getTypes()

    [8] => struct getPublisherAdTagReport {
        string accessToken;
        long publisherId;
        string fromDate;
        string toDate;
        reportingOptionalParams reportingOptionalParams;
    }

根据$ soapClient-&gt; __ getFunctions()的函数getPublisherAdTagReport()如下所示:

    [2] => getPublisherAdTagReportResponse getPublisherAdTagReport(getPublisherAdTagReport $parameters)

1 个答案:

答案 0 :(得分:2)

在上述问题中,这对我有用:

// form an array listing the http header
$httpHeaders = array(
    'http' => array(
        'protocol_version' => 1.1,
        'header' => "Authorization:Bearer " . $access_token . "\r\n",
    ));
// form a stream context
$context = stream_context_create($httpHeaders);
// pass it in an array
$params = array('stream_context' => $context);
$client = new SoapClient("http://beta-api.pubmatic.com/v1/PublisherDemandInsightsService?wsdl",
    $params);
// access the remote method using the client object
$result = $client->getPublisherAdTagReport(array('accessToken' => $access_token, 'publisherId' => $pubId,
                    'fromDate' => $startdate, 'toDate' => $enddate));