googleads / googleads-php-lib

Google Ad Manager SOAP API Client Library for PHP
Apache License 2.0
656 stars 769 forks source link

Type 'ns1:Budget' is not validly derived from the type definition, 'Campaign', of element 'ns1:operand' #255

Closed yooouuri closed 7 years ago

yooouuri commented 7 years ago

Using this package for laravel: https://github.com/nikolajlovenhardt/laravel-google-ads

When i try to create a campaign, the following error occur:

SoapFault in AdsSoapClient.php line 115: Unmarshalling Error: cvc-elt.4.3: Type 'ns1:Budget' is not validly derived from the type definition, 'Campaign', of element 'ns1:operand'.

soap.log: http://pastebin.com/raw/TmU6XSfp

Copied from the "Basic Operations Samples"

$customerClientId = 'xxx-xxx-xxxx';

$adWordsService = new AdWordsService();

/** @var BudgetService $budgetService */
$budgetService = $adWordsService->getService(BudgetService::class, $customerClientId);

// Create the shared budget (required).
$budget = new Budget();
$budget->setName('Interplanetary Cruise Budget #' . uniqid());
$money = new Money();
$money->setMicroAmount(50000000);
$budget->setAmount($money);
$budget->setDeliveryMethod(BudgetBudgetDeliveryMethod::STANDARD);

$operations = [];

// Create a budget operation.
$operation = new BudgetOperation();
$operation->setOperand($budget);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;

// Create the budget on the server.
$result = $budgetService->mutate($operations);
$budget = $result->getValue()[0];

// Create a campaign with only required settings.
$campaign = new Campaign();
$campaign->setName('Interplanetary Cruise #' . uniqid());
$campaign->setAdvertisingChannelType(AdvertisingChannelType::DISPLAY);

// Set shared budget (required).
$campaign->setBudget(new Budget());
$campaign->getBudget()->setBudgetId($budget->getBudgetId());

// Set bidding strategy (required).
$biddingStrategyConfiguration = new BiddingStrategyConfiguration();
$biddingStrategyConfiguration->setBiddingStrategyType(
        BiddingStrategyType::MANUAL_CPC);
$campaign->setBiddingStrategyConfiguration($biddingStrategyConfiguration);

$campaign->setStatus(CampaignStatus::PAUSED);

// Create a campaign operation and add it to the operations list.
$operation = new CampaignOperation();
$operation->setOperand($campaign);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;

/** @var CampaignService $campaignService */
$campaignService = $adWordsService->getService(CampaignService::class, $customerClientId);

$result = $campaignService->mutate($operations);

foreach ($result->getValue() as $campaign) {
    printf("Campaign with name '%s' and ID %d was added.\n",
        $campaign->getName(),
        $campaign->getId()
    );
}

Receiving campaigns with the sample code works...

fiboknacky commented 7 years ago

Hi @yooouuri

First, could you confirm where you got the example code above please? I assume that you wish to create campaigns, but it doesn't look like ours.

In particular, in your code, there is a line for creating $budgetService:

$budgetService = $adWordsService->getService(BudgetService::class, $customerClientId);

But it doesn't look the same as ours:

$budgetService = $adWordsServices->get($session, BudgetService::class);

In addition, we have only AdWordsServices class, not AdWordsService (no s). So, I'm not quite sure if the code you got is correct.

As for the SOAP log, it failed because you've used the campaign service to create a budget, which is not allowed. To solve this, let's clarify first where you got the above code.

Thanks in advance!

Knack

yooouuri commented 7 years ago

Hello @fiboknacky

As you can see, the package does the same.

https://github.com/nikolajlovenhardt/laravel-google-ads/blob/master/src/LaravelGoogleAds/Services/AdWordsService.php#L24

It gives me a service and automatically add the needed credentials.

Thanks for your response.

yooouuri commented 7 years ago

As for the SOAP log, it failed because you've used the campaign service to create a budget, which is not allowed.

I see the problem, needed to clear the $operations = []; array, thanks for the heads up