如何使用PHP中的SoapClient类向XML元素添加其他属性

时间:2014-01-11 12:02:05

标签: php xml soap soap-client bing-api

我无法弄清楚如何使用php为xml doc添加其他属性。

要从Bing Ads Api请求报告,我需要使用SOAP传递信息。我没有任何SOAP经验,所以我在这里通过Ewan Hemmings示例 - http://www.ewanheming.com/bing-ads-api-campaign-download。我需要进行一些微妙的更改,将LastSyncTimeInUTC作为Null传递。

我查看了simpleXML扩展,但我更愿意继续使用Bing文档中使用的SoapClient类。

我一直在使用\ SoapVar - http://www.php.net/manual/en/soapvar.soapvar.php,但我无法让这些例子发挥作用。这是我尝试创建的XML文档的示例,以及我在下面使用的代码。我真正想解释的唯一一点是如何使用SoapVar向LastSyncTimeInUTC添加属性。任何帮助将不胜感激。

            <?xml version="1.0" encoding="UTF-8"?>
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas...../envelope/" xmlns:ns1="http://schem.../Arrays" xmlns:ns2="https://bingads.../v9">

            <SOAP-ENV:Header>
                <ns2:UserName>########</ns2:UserName>
                <ns2:Password>########</ns2:Password>
                <ns2:DeveloperToken>#######</ns2:DeveloperToken>
                <ns2:CustomerAccountId>#######</ns2:CustomerAccountId>
                <ns2:CustomerId>#######</ns2:CustomerId>
            </SOAP-ENV:Header>

            <SOAP-ENV:Body>
                <ns2:DownloadCampaignsByAccountIdsRequest>
                    <ns2:AccountIds>
                        <ns1:long>9869860</ns1:long>
                    </ns2:AccountIds>
                    <ns2:DataScope>EntityPerformanceData</ns2:DataScope>
                    <ns2:DownloadFileType>Csv</ns2:DownloadFileType>
                    <ns2:Entities>Campaigns</ns2:Entities>

                     -- > <LastSyncTimeInUTC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />

                    <ns2:FormatVersion>2.0</ns2:FormatVersion>
                    <ns2:PerformanceStatsDateRange>
                        <ns2:CustomDateRangeEnd>
                            <ns2:Day>1</ns2:Day>
                            <ns2:Month>12</ns2:Month>
                            <ns2:Year>2013</ns2:Year>
                        </ns2:CustomDateRangeEnd>
                        <ns2:CustomDateRangeStart>
                            <ns2:Day>1</ns2:Day>
                            <ns2:Month>9</ns2:Month>
                            <ns2:Year>2013</ns2:Year>
                        </ns2:CustomDateRangeStart>
                    </ns2:PerformanceStatsDateRange>
                </ns2:DownloadCampaignsByAccountIdsRequest>
            </SOAP-ENV:Body>

        </SOAP-ENV:Envelope>


    $options = array(
    "trace" => 1,
    "exceptions" => 0,
    "cache_wsdl" => 0
);

$wsdl = "https://api.bingads.microsoft.com/Api/Advertiser/CampaignManagement/v9/BulkService.svc?wsdl";

$client = new \SoapClient($wsdl, $options);

$headers = array();
$headers[] = new \SoapHeader(API_NAMESPACE, "UserName", $username);
$headers[] = new \SoapHeader(API_NAMESPACE, "Password", $password);
$headers[] = new \SoapHeader(API_NAMESPACE, "DeveloperToken", DEVELOPER_TOKEN);
$headers[] = new \SoapHeader(API_NAMESPACE, "CustomerAccountId", $accountId);
$headers[] = new \SoapHeader(API_NAMESPACE, "CustomerId", $customerId);
$client->__setSoapHeaders($headers);

//$deviceId = new SoapVar('<LastSyncTimeInUTC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />', XSD_ANYXML);

$downloadRequest = array(
    "AccountIds" => array(new \SoapVar($accountId, XSD_LONG, 'xsd:long')),
    "DataScope" => "EntityPerformanceData",
    "DownloadFileType" => "Csv",
    "Entities" => "Campaigns",
    "FormatVersion" => "2.0",

    "PerformanceStatsDateRange" => array(
    "CustomDateRangeEnd"  => array("Day" => "1","Month" => "12","Year" => "2013"),
    "CustomDateRangeStart"  => array("Day" => "1","Month" => "9","Year" => "2013"))
);

$response = $client->DownloadCampaignsByAccountIds($downloadRequest);

echo $client->__getLastRequest();

1 个答案:

答案 0 :(得分:2)

好消息,我知道如何做到这一点。

我并不完全相信它是对的,但确实有效。而且我不太明白它是如何工作的,但这就是我所做的。

======= OLD CODE ====================
$downloadRequest = array(
"AccountIds" => array(new \SoapVar($accountId, XSD_LONG, 'xsd:long')),
"DownloadFileType" => "Tsv",
"Entities" => "Campaigns AdGroups Ads Keywords",
"PerformanceStatsDateRange" => array("PredefinedTime" => "LastFourWeeks")
"FormatVersion"  => "2.0",);

======= NEW CODE ====================
$downloadRequest = new stdClass();
$downloadRequest->AccountIds -> long = new SoapVar($accountId, XSD_LONG, 'xsd:long');
$downloadRequest->DownloadFileType = "Csv";
$downloadRequest->Entities = "Keywords";
$downloadRequest->PerformanceStatsDateRange->PredefinedTime = "LastFourWeeks";
$downloadRequest->FormatVersion = "2.0";
$downloadRequest->LastSyncTimeInUTC = new SoapVar('<ns1:LastSyncTimeInUTC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />', XSD_ANYXML);
相关问题