<?php /** * Copyright 2017 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. */ namespace Google\AdsApi\Examples\AdWords\v201705\BasicOperations; require '../../../../vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201705\cm\CampaignService; use Google\AdsApi\AdWords\v201705\cm\OrderBy; use Google\AdsApi\AdWords\v201705\cm\Paging; use Google\AdsApi\AdWords\v201705\cm\Selector; use Google\AdsApi\AdWords\v201705\cm\SortOrder; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example gets all campaigns. To add a campaign, run AddCampaign.php. */ class GetCampaigns { const PAGE_LIMIT = 500; public static function runExample(AdWordsServices $adWordsServices, AdWordsSession $session) { $campaignService = $adWordsServices->get($session, CampaignService::class); // Create selector. $selector = new Selector(); $selector->setFields(['Id', 'Name']); $selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]); $selector->setPaging(new Paging(0, self::PAGE_LIMIT)); $totalNumEntries = 0; do { // Make the get request. $page = $campaignService->get($selector); // Display results. if ($page->getEntries() !== null) { $totalNumEntries = $page->getTotalNumEntries(); foreach ($page->getEntries() as $campaign) { printf( "Campaign with ID %d and name '%s' was found.\n", $campaign->getId(), $campaign->getName() ); } } // Advance the paging index. $selector->getPaging()->setStartIndex( $selector->getPaging()->getStartIndex() + self::PAGE_LIMIT); } while ($selector->getPaging()->getStartIndex() < $totalNumEntries); printf("Number of results found: %d\n", $totalNumEntries); } 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); } } GetCampaigns::main();
<?php/** * This example gets all campaigns in the account. To add a campaign, run * AddCampaign.php. * * Copyright 2016, 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 * * * 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 GoogleApiAdsAdWords * @subpackage v201705 * @category WebServices * @copyright 2016, Google Inc. All Rights Reserved. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, * Version 2.0 */
// Include the initialization filerequire_once dirname(dirname(__FILE__)) . '/init.php';
/** * Runs the example. * @param AdWordsUser $user the user to run the example with */function GetCampaignsExample(AdWordsUser $user) { // Get the service, which loads the required classes. $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);
// Create selector. $selector = new Selector(); $selector->fields = array('Id', 'Name'); $selector->ordering[] = new OrderBy('Name', 'ASCENDING');
// Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);
do { // Make the get request. $page = $campaignService->get($selector);
// Display results. if (isset($page->entries)) { foreach ($page->entries as $campaign) { printf("Campaign with name '%s' and ID '%s' was found.\n", $campaign->name, $campaign->id); } } else { print "No campaigns were found.\n"; }
// Advance the paging index. $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; } while ($page->totalNumEntries > $selector->paging->startIndex);}
// Don't run the example if the file is being included.if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { return;}
try { // Get AdWordsUser from credentials in "../auth.ini" // relative to the AdWordsUser.php file's directory. $user = new AdWordsUser();
// Log every SOAP XML request and response. $user->LogAll();
// Run the example. GetCampaignsExample($user);} catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage());}
{ "require": { "googleads/googleads-php-lib": "^18.1" }}
{ "name": "googleads/googleads-php-lib", "description": "Google Ads APIs Client Library for PHP (AdWords and DFP)", "require": { "php": ">=5.3.0", "ext-curl": "*", "ext-openssl": "*", "ext-soap": "*" }, "type": "library", "homepage": "https://github.com/googleads/googleads-php-lib", "license": "Apache-2.0", "include-path": ["src/"], "autoload": { "classmap": [ "src/Google/Api/Ads/AdWords/Lib", "src/Google/Api/Ads/Dfp/Lib" ] }, "authors": [ { "name": "Google", } ]}
"require": {
"googleads/googleads-php-lib": "^25.0.0"
}
Thanky you, Shwetha!