如何使用 PHP 中的 SoapClient 类向 XML 元素添加其他属性 [英] How do I add additional attributes to XML Elements with the SoapClient Class in PHP

查看:34
本文介绍了如何使用 PHP 中的 SoapClient 类向 XML 元素添加其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用 php 向 xml 文档添加其他属性.

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

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

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.

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

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

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

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();

推荐答案

好消息,我想出了怎么做.

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);

这篇关于如何使用 PHP 中的 SoapClient 类向 XML 元素添加其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆