2014-01-11 79 views
2

我無法弄清楚如何使用php將其他屬性添加到xml文檔。如何使用PHP中的SoapClient類向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(); 

回答

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); 
相關問題