Get All keywords in Adword v200909 in PHP

I hope this will help you guys when working with adword new API. I just completed this function and thought of sharing it for you all. I couldn’t find any PHP code in the net to do the same.

Here you go…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class GetAllKeywordsExample {
 
    static function main($keywords_input, $match_type) {
        $return_array = array();
        try {
            // Get AdWordsUser from credentials in "../auth.ini"
            // relative to the AdWordsUser.php file's directory.
            $user = new AdWordsUser();
 
            // Log SOAP XML request and response.
            $user->LogDefaults();
 
            // Get the TargetingIdeaService.
            $keywordService = $user->GetTargetingIdeaService();
 
            $keyword = new Keyword();
            $keyword->text = $keywords_input;
            $keyword->matchType = $match_type;
 
            $keyword_array = array($keyword);
            $relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter($keyword_array);
 
            $targetingIdeaSelector = new TargetingIdeaSelector();
            $targetingIdeaSelector->searchParameters = array($relatedToKeywordSearchParameter);
 
            $targetingIdeaSelector->ideaType = 'KEYWORD';
            $targetingIdeaSelector->requestType = 'IDEAS';
            $targetingIdeaSelector->paging = new Paging(0,800);
 
            // Get all Keywords.
            $keywordPage = $keywordService->get($targetingIdeaSelector);
 
            // Prepare keyword array.
            if(isset($keywordPage->entries)) {
                foreach ($keywordPage->entries as $keyword_entry) {
                    foreach($keyword_entry as $keyword_entry_array){
                        foreach ($keyword_entry_array as $keyword){
                            if($keyword->key == 'KEYWORD'){
                                $keyword_value = $keyword->value;
                                if($keyword_value->value->matchType == $match_type){
                                    $return_array[] = array(
                                        'keyword'=>$keyword_value->value->text
                                    );
                                }
                            }
                        }
                    }
                }
            }
            else {
                throw new Exception("ERROR",0);
            }
        } catch (Exception $e) {
            $return_array[] = array(
                'keyword'=>$e->getMessage());
            }
            return $return_array;
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.