IPBLOCK Adwords api php

579 views
Skip to first unread message

man...@devmako.sk

unread,
Feb 6, 2017, 5:00:54 AM2/6/17
to AdWords API Forum

HI all  
I want to block IP but somwhere i have 
problem at code. Can you help me please???
Here is the code

class UpdateKeyword {
const campaignId = '367448090';

 public static function runExample(AdWordsServices $adWordsServices,
      AdWordsSession $session, $campaignId) {
    $campaignCriterionService = $adWordsServices->get($session, CampaignCriterionService::class);
  $negativeCampaignCriterion = new NegativeCampaignCriterion($campaignId,"IsNegative", "IpBlock");

 $negativeCampaignCriterion->Id = "222";
  $negativeCampaignCriterion->ipAddress="10.10.10.10";
 

  // Create operations.
  $operation = new CampaignCriterionOperation();
  $operation->setoperand ($negativeCampaignCriterion);
$operation->setOperator(Operator::ADD);
    $operations[] = $operation;

  // Add campaign criteria.
    $campaignCriterion =
        $campaignCriterionService->mutate($operations)->getValue()[0];

  }

  public static function main() {
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile()
        ->build();

    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new AdWordsSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();


    self::runExample(
        new AdWordsServices(), $session, intval(self::campaignId));
  }
}

UpdateKeyword::main();

thanks

Shwetha Vastrad (AdWords API Team)

unread,
Feb 6, 2017, 3:51:35 PM2/6/17
to AdWords API Forum
Hi,

Could you provide the error that you encounter when you perform this operation? Using the above program, you'll encounter the CampaignCriterionError.CONCRETE_TYPE_REQUIRED. You need to create a concrete criterion of type IpBlock and then provide this to NegativeCampaignCriterion. This example shows how to add a negative keyword criterion at the campaign level. You need to add the negative IpBlock criterion in a similar fashion. Could you try this out and let me know if it works?

Regards,
Shwetha, AdWords API Team.

SK. MOHTASIM BILLAH

unread,
Jul 19, 2017, 1:46:44 AM7/19/17
to AdWords API Forum
Hi,

I am a asp.net developer and I am trying to develop the same thing (ip block) from my application.I am using .net client library and I can run the usual campaigns like campaign add, retrieve, keywords retrieve etc. If you were able to solve the issue then can you help me with the code. PHP or asp.net, either one will do if you can provide me.

Thanks
SK. Mohtasim Billah

Shwetha Vastrad (AdWords API Team)

unread,
Jul 19, 2017, 11:52:58 AM7/19/17
to AdWords API Forum
Hi, 

You can use the PHP code snippet below to add a negative IpBlock criterion to your campaign.

class AddIpBlock {

 
const CAMPAIGN_ID = 'YOUR_CAMPAIGN_ID';


 
public static function runExample(AdWordsServices $adWordsServices,

     
AdWordsSession $session, $campaignId, $locationFeedId) {

    $campaignCriterionService
=
        $adWordsServices
->get($session, CampaignCriterionService::class);


    $campaignCriteria
= [];

   
// Add a negative campaign criterion.
    $ipBlock
= new IpBlock();
    $ipBlock
->setIpAddress('IpAddress');
    $negativeCriterion
= new NegativeCampaignCriterion();
    $negativeCriterion
->setCampaignId($campaignId);
    $negativeCriterion
->setCriterion($ipBlock);

    $operation
= new CampaignCriterionOperation();
    $operation
->setOperator(Operator::ADD);
    $operation
->setOperand($negativeCriterion);
    $operations
[] = $operation;

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

   
// Print out some information about added campaign criteria.
   
foreach ($result->getValue() as $campaignCriterion) {
      printf
(
         
"Campaign targeting criterion with ID %d and type '%s' was added.\n",
          $campaignCriterion
->getCriterion()->getId(),
          $campaignCriterion
->getCriterion()->getType());

   
}
 
}

 
public static function main() {
   
// Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential
= (new OAuth2TokenBuilder())
       
->fromFile()
       
->build();

   
// Construct an API session configured from a properties file and the OAuth2
   
// credentials above.
    $session
= (new AdWordsSessionBuilder())
       
->fromFile()
       
->withOAuth2Credential($oAuth2Credential)
       
->build();
   
self::runExample(

       
new AdWordsServices(), $session, intval(self::CAMPAIGN_ID));
 
}
}

AddIpBlock::main();

eit...@eitsec.com

unread,
Mar 26, 2018, 2:11:07 AM3/26/18
to AdWords API Forum
REMOVE is not working

Dhanya Sundararaju (AdWords API Team)

unread,
Mar 26, 2018, 11:39:17 AM3/26/18
to AdWords API Forum
Hi,

In order for us to track issues better, could you please elaborate your question in a new thread as this thread is quite old? 

Regards,
Dhnaya, AdWords API Team

rajit bansal

unread,
Jan 9, 2019, 1:28:31 PM1/9/19
to AdWords API and Google Ads API Forum
Can u recode this in structural Approach?

Dhanya Sundararaju (AdWords API Team)

unread,
Jan 9, 2019, 5:34:49 PM1/9/19
to AdWords API and Google Ads API Forum
Hi,

In order for us to track issues better, could you please elaborate your question in a new thread? 

Regards,
Dhanya, AdWords API Team

Reply all
Reply to author
Forward
0 new messages