My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* This example generates ad code for an AFS search box.
*
* PHP version 5
* PHP extensions: SoapClient and OpenSSL.
*
* Copyright 2011, 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.
*
* @package GoogleApiAdsAdSense
* @subpackage v3
* @category WebServices
* @copyright 2011, Google Inc. All Rights Reserved.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @author Sergio Gomes <api.sgomes@gmail.com>
* @link http://code.google.com/apis/adsense/developer/AdSenseForSearchService.html#generateSearchBoxCode
*/

error_reporting(E_STRICT | E_ALL);

// Provide AdSense login information.
$developer_email = 'INSERT_DEVELOPER_EMAIL_HERE';
$developer_password = 'INSERT_DEVELOPER_PASSWORD_HERE';
$display_locale = 'en_US';
$client_id = 'INSERT_PUBLISHER_ID_HERE';

// Set SOAP and XML settings. To send requests to the production environment,
// replace "sandbox.google.com" with "www.google.com" in the wsdl URL.
// The namespace will always be "www.google.com", even in the sandbox.
$wsdl =
'https://sandbox.google.com/api/adsense/v3/AdSenseForSearchService?wsdl';
$namespace = 'http://www.google.com/api/adsense/v3';
$options = array(
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'encoding' => 'utf-8',
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
'user_agent' => 'PHP-SOAP/'. phpversion() . ',gzip',
// We're setting WSDL caching so that we don't hammer the servers retrieving
// the WSDL for every run. If caching causes any odd behaviour for you, set
// this to WSDL_CACHE_NONE, download the WSDL to your disk, and set $wsdl to
// the file's location.
'cache_wsdl' => WSDL_CACHE_BOTH);

// Get AdSenseForContentService.
$afs_service = new SoapClient($wsdl, $options);

// Set headers.
$developer_email_header = new SoapHeader($namespace, 'developer_email',
$developer_email);
$developer_password_header = new SoapHeader($namespace, 'developer_password',
$developer_password);
$display_locale_header = new SoapHeader($namespace, 'display_locale',
$display_locale);
$client_id_header = new SoapHeader($namespace, 'client_id', $client_id);
$afs_service->__setSoapHeaders(array($developer_email_header,
$developer_password_header, $display_locale_header, $client_id_header));

// Generate ad code.
try {
$result = $afs_service->generateSearchBoxCode(array(
'synServiceId' => $client_id,
'country' => 'US',
'searchType' => array('value' => 'GoogleSearch'),
'siteProperties' => array(
'encoding' => '',
'locale' => 'en'),
'searchOptions' => array(
'isSafeSearch' => true,
'showResultsInNewWindow' => false),
'domains' => array(''),
'searchBoxStyle' => array(
'backgroundColor' => '#FFFFFF',
'isButtonBelow' => true,
'isCustomStyle' => false,
'isLogoAbove' => true,
'logoType' => array('value' => 'GoogleLogo'),
'searchStyleName' => 'Blue Sky',
'textBoxLength' => 30,
'textColor' => '#000000')));
} catch (SoapFault $e) {
if (isset($e->detail) && isset($e->detail->AdSenseApiExceptionFault) &&
isset($e->detail->AdSenseApiExceptionFault->AdSenseApiException)) {
// Caught AdSense API exception.
$inner = $e->detail->AdSenseApiExceptionFault->AdSenseApiException;
switch ($inner->code) {
case 314:
print "The provided publisher ID was not valid.\n";
break;
default:
print "An error occurred while generating search box code:\n";
print $inner->message;
print "\n";
}
} else {
// Caught other type of exception.
print "Unexpected error while generating search box code.\n";
print $e->getMessage() . "\n";
}
exit(1);
}

// Display search box code.
if (isset($result->return)) {
print "Generated search box code:\n" . $result->return . "\n";
}

Change log

r110 by silvano.luciani on Mar 13, 2012   Diff
Added missing parameter to search box
example
Go to: 
Project members, sign in to write a code review

Older revisions

r109 by api.sgomes on Jul 27, 2011   Diff
Adding final standalone PHP examples
All revisions of this file

File info

Size: 4490 bytes, 117 lines

File properties

svn:executable
*
Powered by Google Project Hosting