php - 如何使用 PHP 中的 SoapClient 类向 XML 元素添加附加属性

标签 php xml soap soap-client bing-api

我无法弄清楚如何使用 php 将附加属性添加到 xml 文档。

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

最佳答案

好消息,我知道怎么做了。

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

======= 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 - 如何使用 PHP 中的 SoapClient 类向 XML 元素添加附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061982/

相关文章:

python - 解析 bit.ly 的 XML 响应

java - Scala 中的 SOAP 代理——我需要什么?

php - 每行水平滚动 div

PHP Post数组数组

php - 需要有关 php 中调节值的帮助

c# - 如何区分WCF数据契约中用户指定的空值和默认值

java - Soap 错误 : XML validation error on request: cvc-complex-type. 4:属性 'Id' 必须出现在元素上

PHP - 检查用户输入的日期是 future 的还是今天的

python - 使用 python ElementTree : ParseError: mismatched tag 解析 XML

php - 使用 php 从变量打印 gif