#!/usr/bin/perl
#
# Copyright 2009, Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code sample retrieves variations for a seed keyword.

use strict;
use warnings;
use English '-no_match_vars';
use SOAP::Lite;

binmode(STDOUT, ':utf8');

# Provide AdWords login information.
		my $email = 'shrigondekar.amit@gmail.com';
		my $password = 'dummy';
		my $client_email = 'client_3+shrigondekar.amit@gmail.com';
		my $useragent = 'Sample';
		my $developer_token = 'shrigondekar.amit@gmail.com++USD';
		my $application_token = '';
		
		

		my $keyword = 'monkey';
		my $match_type = 'BROAD'; 
        my $LANGUAGE = 'en'; 
        my $COUNTRY = 'US'; 
        my $service; 
        my $namespace; 

# Set up service connection with autotyping disabled and fault handler
# registered. To send requests to production environment, replace "sandbox" with
# "adwords". To view XML request/response, uncomment
SOAP::Lite->import(+trace => 'debug');
my $url = sprintf('https://adwords-sandbox.google.com/api/adwords/o/v200909/TargetingIdeaService');
my $wsdl = $url . '?wsdl';

# https://adwords.google.com/api/adwords/o/v200909/TargetingIdeaService?wsdl 
	
	
 $service = SOAP::Lite->service($wsdl)->autotype(0)->readable(1)->proxy($url);
$service->on_fault(sub {
                     my $response = $ARG[1];
                     die('The following SOAP fault occurred:', "\n",
                         '  faultcode: ', $response->faultcode(), "\n",
                         '  faultstring: ', $response->faultstring(), "\n")
                   });
SOAP::Lite->import(+trace => 'debug');

# Define SOAP headers.
my @headers = (
  SOAP::Header->name('email' => $email),
  SOAP::Header->name('password' => $password),
  SOAP::Header->name('clientEmail' => $client_email),
  SOAP::Header->name('useragent' => $useragent),
  SOAP::Header->name('developerToken' => $developer_token),
  SOAP::Header->name('applicationToken' => $application_token)
);


my $soap_header = SOAP::Header->name('RequestHeader' =>\SOAP::Header->value(
         SOAP::Header->name('email' => $email),
		 SOAP::Header->name('password' => $password),
         SOAP::Header->name('clientEmail'      => $client_email),
         SOAP::Header->name('userAgent'        => $useragent),
         SOAP::Header->name('developerToken'   =>$developer_token),
         SOAP::Header->name('applicationToken' =>$application_token),
         SOAP::Header->name('alternateUrl'     => 'https://adwords-sandbox.google.com'),
       ));

# Create seed keyword structure.
my $selector = SOAP::Data->name('selector' => {
             'searchParameters' => [
               {
                 'type' => 'RelatedToKeywordSearchParameter',
                 'keywords' => [
                   {
                     'text' => 'flowers',
                     'matchType' => 'BROAD',
                   },
                 ]
               },
               {
                 'type' => 'LanguageTargetSearchParameter',
                 'languageTargets' => [
                   {'languageCode' => $LANGUAGE},
                 ]
               },
               {
                 'type' => 'CountryTargetSearchParameter',
                 'countryTargets' => [
                   {'countryCode' => $COUNTRY},
                 ]
               },
             ],
             'ideaType'         => 'KEYWORD',
             'requestType'      => 'IDEAS',
             'paging'           => {
               'startIndex'     => '0',
               'numberResults'  => '10',
             }
           });
		   
		    my $som = '';
           my $som_results = '';
           eval {
             $som = $service->get('TargetingIdeaSelector'=>$selector,
 $soap_header);
             if (defined($som) && $som) {
               $som_results = $som->result();
             }
           };
           if ($@)  {
             print "Error while making SOAP request for keyword\n";
           }
		   
		   
