How do I add additional attributes to XML Elements

2019-05-28 12:15发布

I cannot work out how to add additional attributes to an xml doc using php.

To request a report from the Bing Ads Api I need to pass the information with SOAP. I don't have any SOAP experience so I am working through Ewan Hemmings Example here - http://www.ewanheming.com/bing-ads-api-campaign-download. I need to make some subtle changes that will pass the LastSyncTimeInUTC as Null.

I have looked at simpleXML extension but I would prefer to continue using the SoapClient Class used in the Bing Documentation.

I have been messing around with \SoapVar - http://www.php.net/manual/en/soapvar.soapvar.php but I cant get the examples to work. This is an example of the XML doc I am trying to create, and the code I am using below. The only bit I really want explaining is how to use SoapVar to add an attribute to LastSyncTimeInUTC. Any help would be appreciated.

            <?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条回答
家丑人穷心不美
2楼-- · 2019-05-28 13:01

Good News, I worked out how to do it.

I'm not entirely confident it's right, but it does work. And I don't quite understand how it works yet but this is what I did.

======= 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);
查看更多
登录 后发表回答