adCenter API implementation with nusoap in PHP

Hi,

Currently, I am implementing adcenter API in PHP using nusoap tool kit. I already had my sandbox environment activated. Here's my problem.

<?php

require_once('nusoap.php');
$parameters=array("APIFlags"=>1);
//msnapi.wsdl is a copy of https://beta6.api.idss.msn.com/adcenterapi.asmx?wsdl

$soapclient = new nusoapclient('msnapi.wsdl','wsdl');

if ($soapclient->call('GetAccounts',$parameters))
echo "success";
else
echo "false";
?>

When I executed the above script, It displays false. What could be the reason ? I am new to adCenter API and SOAP. any code samples available for PHP ?

Please advice.

Thanks,
Hari

[746 byte] By [hariarla] at [2008-3-3]
# 1
The API needs authentication so you need to use the setHeaders method with the credentials you were given.

Something like that:

<?php

$headers = '
<ApiUserAuthHeader>
<UserName>your usernameUserName>
<Password>your password</Password>
<UserAccessKey>your key</UserAccessKey>
</ApiUserAuthHeader>';

$nusoap->setHeaders($headers);

#and the call to the call method here
?>

JulienDephix at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 2

Hi Julien,

This solves my issue and now I am able to get campaigns. But I have one more issue..I am trying to create a campaign by using below script:

<?php
$SOAPClient = getClient();
//set up campaign args
$campaignArray[] = new soapval('AdCenterCampaign', false,
array(
"CampaignId" => 0,
"CampaignName" => $txtName,
"CampaignDesc" => $txtDesc,
"BudgetAmt" => $txtAmount,
"BudgetLimitType" => $cboBudgetType,
"DaylightSavingFlag" => "true",
"TimeZoneType" => "CentralTime_US_Canada"
), false, false);
$arguments = array("APIFlags" => 0,
"AccountId" => 592,
"Campaigns" => $campaignArray);
//execute campaign call
$data = callAPI($SOAPClient, 'AddCampaigns', $arguments);
echo "<pre>";
print_r($data);
?>

Here's the output of this script:
Array
(
[faultcode] => soap:Server
[faultstring] => Server was unable to process request. > |-400025|-1|ERROR_API_NULL_CAMPAIGNS_INFORMATION|
[detail] =>
)

I checked the posted values and they are fine. Whats could be the reason for this error ? please advice.

Thanks,
Hari

hariarla at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 3
Hi,

sorry for the late reply, maybe you've sorted this out by now...

I personally write the XML code myself rather than using the soapval method.

Can you post what print_r($campaignArray) and print_r($_POST) returns?

I suspect either a problem with the data you are passing to soapval or the value this method returns.

If needed, I will post some code that works for me.

Cheers.

JulienDephix at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 4
Julien,

I'm having the same problem as the above person. Here's a code snippet from what I'm trying to do...

$toMSN = array();
foreach($campaigns as $a => $b) {
$toMSN[] = array(
"CampaignName" => $b["name"],
"CampaignDesc" => $b["name"],
"BudgetAmt" => $_POST["budget".$b["id"]],
"BudgetLimitType" => $_POST["budgettype".$b["id"]],
"DaylightSavingFlag" => false,
"TimeZoneType" => "CentralTime_US_Canada"
);
}
}

require_once("nusoap/nusoap.php");
$msnClient = new SoapClient(MSN_WSDL_CAMPAIGN, "wsdl");

$headers = "<ApiUserAuthHeader xmlns=\"http://adcenter.microsoft.com/syncapis\"><UserName>XXXX</UserName><Password>YYYY</Password><UserAccessKey>ZZZZ</UserAccessKey></ApiUserAuthHeader>";

$parameters = array(
"APIFlags"=>0,
"AccountId"=>$client["acct_msn"],
"Campaigns"=>$toMSN
);

$msnClient->setHeaders($headers);
$msnClient->call("AddCampaigns", $parameters);

Here's a print_r output of the $toMSN after it looped through two campaigns and tried to send them.

Array
(
[0] => Array
(
[CampaignName] => Campaign1
[CampaignDesc] => Campaign1
[BudgetAmt] => 50
[BudgetLimitType] =>
[DaylightSavingFlag] =>
[TimeZoneType] => CentralTime_US_Canada
)

[1] => Array
(
[CampaignName] => Campaign2
[CampaignDesc] => Campaign2
[BudgetAmt] => 50
[BudgetLimitType] =>
[DaylightSavingFlag] =>
[TimeZoneType] => CentralTime_US_Canada
)

)Same deal as the guy above. It always returns that null campaigns error. And like him, I'm very new to SOAP. So any help you can provide would be most appreciated. :)

Thanks in advance.

JaminBlount at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 5

Hi Jamin,

have you figured out the issue ? if not, find the below snippet which worked for me. This is for production environment.

<?php

//Create the client
$SOAPClient = new nusoapclient("
https://adcenterapi.microsoft.com/v3/CampaignManagement/CampaignManagement.asmx?wsdl");

$SOAPACTION = "http://adcenter.microsoft.com/syncapis/AddCampaigns";
$BODY = '<AddCampaigns xmlns="
http://adcenter.microsoft.com/syncapis"><APIFlags>0</APIFlags><AccountId>3434</AccountId>
<Campaigns><AdCenterCampaign><CampaignId>0</CampaignId>
<CampaignName>somejunk</CampaignName>
<CampaignDesc>somejunk</CampaignDesc>
<MonthlyBudgetAmt>34.00</MonthlyBudgetAmt>
<BudgetType>SpendUntilDepleted</BudgetType>
<DaylightSavingFlag>true</DaylightSavingFlag> <TimeZone>PacificTime_US_Canada_Tijuana</TimeZone></AdCenterCampaign></Campaigns></AddCampaigns>';

$HEADER ='<ApiUserAuthHeader xmlns="http://adcenter.microsoft.com/syncapis">
<UserName>xxxxx</UserName>
<UserAccessKey>yyyyy</UserAccessKey>
<Password>zzzzz</Password>
</ApiUserAuthHeader>';
$SOAPMESSAGE = $SOAPClient->serializeEnvelope($BODY,$HEADER,array(),'document', 'literal');
$RESULT = $SOAPClient->send($SOAPMESSAGE, $SOAPACTION);
print_r($RESULT);
?>

Let me know if this works for you.

Cheers,
Hari Arla

hariarla at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 6
I've found that I have to include a 'parameters' key in nusoap arrays sometimes. Here's how I set up my AddCampaigns array for nusoap:

<pre>
$params = array(
'APIFlags' => 0,
'AccountId' => $this->account_id,
'Campaigns' => array(
'AdCenterCampaign' => array(
'CampaignName' => $this->name,
'CampaignDesc' => $this->description,
'MonthlyBudgetAmt' => $this->budget,
'BudgetType' => $this->budget_type,
'DaylightSavingsFlag' => $this->daylight_savings ? 'true' : 'false',
'TimeZone' => $this->timezone
)
)
);
$params = array('parameters' => $params);
</pre>

notice the last line.

I still get an error when sending this (error -19999) but when I look at what nusoap is sending, it conforms to the example XML i've seen.

jlipps at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 7

After seeing your params array, I realized that you are missing campaignId entry in adcentercampaign array. This should set to zero for new campaigns.

Thanks,
Hari

hariarla at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 8
Thanks Hari, but it does not solve the error -199999 issue for me.
jlipps at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 9
Hari,

Thanks for the response. Sorry, I've been out of the office all week and haven't been able to get back in here. I'm still having problems. I tried using your snippet, to manually create the XML instead of letting nusoap create it from an array. But when I do that, your $RESULT variable comes back completely empty.

If I switch it to print the actual soap response (in my case, $msnClient->response) this is what I get:

HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Date: Fri, 10 Nov 2006 19:50:00 GMT
Server: Microsoft-IIS/6.0
P3P:CP="BUS CUR CONo FIN IVDo ONL OUR PHY SAMo TELo"
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Length: 0And this is the full request that I'm sending, that's somehow resulting in the Bad Request error:

OST /v3/CampaignManagement/CampaignManagement.asmx?wsdl HTTP/1.0
Host: beta6.api.idss.msn.com
User-Agent: NuSOAP/0.7.2 (1.94)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "http://adcenter.microsoft.com/syncapis/AddCampaigns"
Content-Length: 1078

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ApiUserAuthHeader xmlns="http://adcenter.microsoft.com/syncapis">
<UserName>XXXX</UserName>
<Password>YYYY</Password>
<UserAccessKey>ZZZZ</UserAccessKey>
</ApiUserAuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<AddCampaigns xmlns="http://adcenter.microsoft.com/syncapis">
<APIFlags>0</APIFlags>
<AccountId>AAAAA</AccountID>
<Campaigns>
<AdCenterCampaign>
<CampaignID>0</CampaignID>
<CampaignName>Campaign1</CampaignName>
<CampaignDesc>Campaign1</CampaignDesc>
<BudgetAmt>50</BudgetAmt>
<BudgetLimitType></BudgetLimitType>
<DaylightSavingFlag>false</DaylightSavingFlag>
<TimeZoneType>CentralTime_US_Canada</TimeZoneType>
</AdCenterCampaign>
</Campaigns>
</AddCampaigns>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I am using the sandbox, rather than the production environment, but I didn't think there was any functional difference. If you can spot anything wrong with my XML, please let me know, as I'm totally stuck now. Nothing seems to work.

Thanks again for your help.

JaminBlount at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 10
Should not it be <MonthlyBudgetAmt>50</MonthlyBudgetAmt> instead of <BudgetAmt>50</BudgetAmt> ?
Ludo-R at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 11
Ok, I changed that. Thanks. But it's still giving the Bad Request http error. :-\

Jamin

JaminBlount at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 12
Shouldn't the amount be a double? 50.00 instead of 50 (I don't know if it makes any difference).
Ludo-R at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...
# 13
Hi guys, sorry I haven't checked the forum recently! My bad!

The problem is that you make a mistake between the tag name and its type.

Look at the following line:
<s:element minOccurs="1" maxOccurs="1" name="BudgetType" type="tns:BudgetLimitType"/>

Your tag should be called "BudgetType" and its type is "BudgetLimitType".
Then you look in the WSDL under "BudgetLimitType" and you find:

<s:simpleType name="BudgetLimitType">
<s:restriction base="s:string">
<s:enumeration value="DailyLimit"/>
<s:enumeration value="NoDailyLimit"/>
</s:restriction>
</s:simpleType>

which tells you the available values are "DailyLimit" and "NoDailyLimit".

Same goes for "TimeZone" which is not "TimeZoneType".

Your XML would then look like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ApiUserAuthHeader xmlns="http://adcenter.microsoft.com/syncapis">
<UserName>XXXX</UserName>
<Password>YYYY</Password>
<UserAccessKey>ZZZZ</UserAccessKey>
</ApiUserAuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<AddCampaigns xmlns="http://adcenter.microsoft.com/syncapis">
<APIFlags>0</APIFlags>
<AccountId>AAAAA</AccountID>
<Campaigns>
<AdCenterCampaign>
<CampaignName>Campaign1</CampaignName>
<CampaignDesc>Campaign1</CampaignDesc>
<BudgetAmt>50</BudgetAmt>
<BudgetType>DailyLimit</BudgetType>
<DaylightSavingFlag>false</DaylightSavingFlag>
<TimeZone>CentralTime_US_Canada</TimeZone>
</AdCenterCampaign>
</Campaigns>
</AddCampaigns>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

By the way, you don't necessarily need to set the campaign ID to 0, it just works without.

I also use $msnClient->debug_str (or is it str_debug?) which contains everything NuSOAP does which makes it easier to debug.

Hope that helps.

JulienDephix at 2007-9-4 > top of Msdn Tech,Windows Live Developer Forums,Microsoft adCenter: Development...

Windows Live Developer Forums

Site Classified