Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to latest stable version of Google API Client Library (0.6.1)
- Loading branch information
Showing
38 changed files
with
14,033 additions
and
4,666 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
google-api-php-client/examples/adsensehost/AdSenseHostAuth.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/* | ||
* Copyright 2012 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// Error if PDO and PDO_SQLITE not present | ||
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { | ||
throw new Exception('The sample code needs PDO and PDO_SQLITE PHP extension'); | ||
} | ||
|
||
/** | ||
* Include the library files for the api client and AdSense service class. | ||
*/ | ||
require_once "../../src/Google_Client.php"; | ||
require_once "../../src/contrib/Google_AdsensehostService.php"; | ||
|
||
/** | ||
* Handles authentication and OAuth token storing. | ||
* Assumes the presence of a sqlite database called './examples.sqlite' | ||
* containing a table called 'auth' composed of two VARCHAR(255) fields called | ||
* 'user' and 'token'. | ||
* | ||
* @author Sérgio Gomes <sgomes@google.com> | ||
* @author Silvano Luciani <silvano.luciani@gmail.com> | ||
*/ | ||
|
||
class AdSenseHostAuth { | ||
protected $apiClient; | ||
protected $adSenseHostService; | ||
private $user; | ||
|
||
/** | ||
* Create the dependencies. | ||
* (Inject them in a real world app!!) | ||
*/ | ||
public function __construct() { | ||
// Create the Google_Client instance. | ||
// You can set your credentials in the config.php file, included under the | ||
// src/ folder in your client library install. | ||
$this->apiClient = new Google_Client(); | ||
// Create the api AdsensehostService instance. | ||
$this->adSenseHostService = new Google_AdsensehostService($this->apiClient); | ||
} | ||
|
||
/** | ||
* Check if a token for the user is already in the db, otherwise perform | ||
* authentication. | ||
* @param string $user The user to authenticate | ||
*/ | ||
public function authenticate($user) { | ||
$this->user = $user; | ||
$dbh = new PDO('sqlite:examples.sqlite'); | ||
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
$stmt = $dbh->prepare('CREATE TABLE IF NOT EXISTS auth ' . | ||
'(user VARCHAR(255), token VARCHAR(255))'); | ||
$stmt->execute(); | ||
$token = $this->getToken($dbh); | ||
if (isset($token)) { | ||
// I already have the token. | ||
$this->apiClient->setAccessToken($token); | ||
} else { | ||
// Override the scope to use the readonly one | ||
$this->apiClient->setScopes( | ||
array("https://www.googleapis.com/auth/adsensehost")); | ||
// Go get the token | ||
$this->apiClient->setAccessToken($this->apiClient->authenticate()); | ||
$this->saveToken($dbh, false, $this->apiClient->getAccessToken()); | ||
} | ||
$dbh = null; | ||
} | ||
|
||
/** | ||
* Return the AdsenseService instance (to be used to retrieve data). | ||
* @return apiAdsenseService the authenticated apiAdsenseService instance | ||
*/ | ||
public function getAdSenseHostService() { | ||
return $this->adSenseHostService; | ||
} | ||
|
||
/** | ||
* During the request, the access code might have been changed for another. | ||
* This function updates the token in the db. | ||
*/ | ||
public function refreshToken() { | ||
if ($this->apiClient->getAccessToken() != null) { | ||
$dbh = new PDO('sqlite:examples.sqlite'); | ||
$this->saveToken($dbh, true, $this->apiClient->getAccessToken()); | ||
} | ||
} | ||
|
||
/** | ||
* Insert/update the auth token for the user. | ||
* @param PDO $dbh a PDO object for the local authentication db | ||
* @param bool $userExists true if the user already exists in the db | ||
* @param string $token the auth token to be saved | ||
*/ | ||
private function saveToken($dbh, $userExists, $token) { | ||
if ($userExists) { | ||
$stmt = $dbh->prepare('UPDATE auth SET token=:token WHERE user=:user'); | ||
} else { | ||
$stmt = $dbh | ||
->prepare('INSERT INTO auth (user, token) VALUES (:user, :token)'); | ||
} | ||
$stmt->bindParam(':user', $this->user); | ||
$stmt->bindParam(':token', $this->apiClient->getAccessToken()); | ||
$stmt->execute(); | ||
} | ||
|
||
/** | ||
* Retrieves token for use. | ||
* @param PDO $dbh a PDO object for the local authentication db | ||
* @return string a JSON object representing the token | ||
*/ | ||
private function getToken($dbh) { | ||
$stmt = $dbh->prepare('SELECT token FROM auth WHERE user= ?'); | ||
$stmt->execute(array($this->user)); | ||
$row = $stmt->fetch(PDO::FETCH_ASSOC); | ||
return $row['token']; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
google-api-php-client/examples/adsensehost/examples/AddAdUnitToPublisher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/* | ||
* Copyright 2012 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// Require the base class. | ||
require_once __DIR__ . "/../BaseExample.php"; | ||
|
||
/** | ||
* This example adds a new ad unit to a publisher ad client. | ||
* | ||
* To get ad clients, see GetAllAdClientsForPublisher.php. | ||
* | ||
* Tags: accounts.adunits.insert | ||
* | ||
* @author Sérgio Gomes <sgomes@google.com> | ||
*/ | ||
class AddAdUnitToPublisher extends BaseExample { | ||
public function render() { | ||
$accountId = PUBLISHER_ACCOUNT_ID; | ||
$adClientId = PUBLISHER_AD_CLIENT_ID; | ||
|
||
$adUnit = new Google_AdUnit(); | ||
$adUnit->setName(sprintf('Ad Unit #%s', $this->getUniqueName())); | ||
|
||
$contentAdsSettings = new Google_AdUnitContentAdsSettings(); | ||
$backupOption = new Google_AdUnitContentAdsSettingsBackupOption(); | ||
$backupOption->setType('COLOR'); | ||
$backupOption->setColor('ffffff'); | ||
$contentAdsSettings->setBackupOption($backupOption); | ||
$contentAdsSettings->setSize('SIZE_200_200'); | ||
$contentAdsSettings->setType('TEXT'); | ||
$adUnit->setContentAdsSettings($contentAdsSettings); | ||
|
||
$customStyle = new Google_AdStyle(); | ||
$colors = new Google_AdStyleColors(); | ||
$colors->setBackground('ffffff'); | ||
$colors->setBorder('000000'); | ||
$colors->setText('000000'); | ||
$colors->setTitle('000000'); | ||
$colors->setUrl('0000ff'); | ||
$customStyle->setColors($colors); | ||
$customStyle->setCorners('SQUARE'); | ||
$font = new AdStyleFont(); | ||
$font->setFamily('ACCOUNT_DEFAULT_FAMILY'); | ||
$font->setSize('ACCOUNT_DEFAULT_SIZE'); | ||
$customStyle->setFont($font); | ||
$adUnit->setCustomStyle($customStyle); | ||
|
||
// Retrieve custom channels list, and display it. | ||
$result = $this->adSenseHostService->accounts_adunits | ||
->insert($accountId, $adClientId, $adUnit); | ||
$mainFormat = | ||
'Ad unit w/ ID "%s", type "%s", name "%s" and status "%s" was created.'; | ||
$content = sprintf($mainFormat, $result['id'], | ||
$result['contentAdsSettings']['type'], $result['name'], | ||
$result['status']); | ||
print $content; | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
google-api-php-client/examples/adsensehost/examples/AddCustomChannelToHost.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/* | ||
* Copyright 2012 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// Require the base class. | ||
require_once __DIR__ . "/../BaseExample.php"; | ||
|
||
/** | ||
* This example adds a custom channel to a host ad client. | ||
* | ||
* To get ad clients, see GetAllAdClientsForHost.php. | ||
* Tags: customchannels.insert | ||
* | ||
* @author Sérgio Gomes <sgomes@google.com> | ||
*/ | ||
class AddCustomChannelToHost extends BaseExample { | ||
public function render() { | ||
$adClientId = HOST_AD_CLIENT_ID; | ||
|
||
$customChannel = new Google_CustomChannel(); | ||
$customChannel->setName(sprintf('Sample Channel #%s', | ||
$this->getUniqueName())); | ||
|
||
// Retrieve custom channels list, and display it. | ||
$result = $this->adSenseHostService->customchannels | ||
->insert($adClientId, $customChannel); | ||
$mainFormat = | ||
'Custom channel with ID "%s", code "%s" and name "%s" was created.'; | ||
$content = sprintf($mainFormat, $result['id'], $result['code'], | ||
$result['name']); | ||
print $content; | ||
} | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
google-api-php-client/examples/adsensehost/examples/AddUrlChannelToHost.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/* | ||
* Copyright 2012 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// Require the base class. | ||
require_once __DIR__ . "/../BaseExample.php"; | ||
|
||
/** | ||
* This example adds a URL channel to a host ad client. | ||
* | ||
* To get ad clients, see GetAllAdClientsForHost.php. | ||
* Tags: urlchannels.insert | ||
* | ||
* @author Sérgio Gomes <sgomes@google.com> | ||
*/ | ||
class AddUrlChannelToHost extends BaseExample { | ||
public function render() { | ||
$adClientId = HOST_AD_CLIENT_ID; | ||
|
||
$urlChannel = new Google_UrlChannel(); | ||
$urlChannel->setUrlPattern(sprintf('www.example.com/%s', | ||
$this->getUniqueName())); | ||
|
||
// Retrieve URL channels list, and display it. | ||
$result = $this->adSenseHostService->urlchannels | ||
->insert($adClientId, $urlChannel); | ||
$mainFormat = 'URL channel with id "%s" and URL pattern "%s" was created.'; | ||
$content = sprintf($mainFormat, $result['id'], $result['urlPattern']); | ||
print $content; | ||
} | ||
} | ||
|
Oops, something went wrong.