PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/googleads-php-lib/examples/Dfp/v201605/PublisherQueryLanguageService/GetAllBrowsers.php

https://github.com/markvince/CakePHP-GAStats-Plugin
PHP | 108 lines | 45 code | 15 blank | 48 comment | 2 complexity | 7973c0b07097ee009f208899bae129f4 MD5 | raw file
  1. <?php
  2. /**
  3. * This example gets all browsers available to target from the Browser table.
  4. * Other tables include 'Bandwidth_Group', 'Browser_Language',
  5. * 'Device_Capability', 'Operating_System', etc. This example may take a while
  6. * to run.
  7. *
  8. * NOTE: Since this example loads all results into memory, your PHP memory_limit
  9. * may need to be raised for this example to work properly.
  10. *
  11. * A full list of available criteria tables can be found at:
  12. * https://developers.google.com/doubleclick-publishers/docs/reference/v201605/PublisherQueryLanguageService
  13. *
  14. * PHP version 5
  15. *
  16. * Copyright 2014, Google Inc. All Rights Reserved.
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. *
  30. * @package GoogleApiAdsDfp
  31. * @subpackage v201605
  32. * @category WebServices
  33. * @copyright 2014, Google Inc. All Rights Reserved.
  34. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  35. * Version 2.0
  36. */
  37. error_reporting(E_STRICT | E_ALL);
  38. // You can set the include path to src directory or reference
  39. // DfpUser.php directly via require_once.
  40. // $path = '/path/to/dfp_api_php_lib/src';
  41. $path = dirname(__FILE__) . '/../../../../src';
  42. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  43. require_once 'Google/Api/Ads/Dfp/Lib/DfpUser.php';
  44. require_once 'Google/Api/Ads/Dfp/Util/v201605/Pql.php';
  45. require_once 'Google/Api/Ads/Dfp/Util/v201605/StatementBuilder.php';
  46. require_once dirname(__FILE__) . '/../../../Common/ExampleUtils.php';
  47. try {
  48. // Get DfpUser from credentials in "../auth.ini"
  49. // relative to the DfpUser.php file's directory.
  50. $user = new DfpUser();
  51. // Log SOAP XML request and response.
  52. $user->LogDefaults();
  53. // Get the PublisherQueryLanguageService.
  54. $pqlService = $user->GetService('PublisherQueryLanguageService', 'v201605');
  55. // Create statement to select all line items.
  56. $statementBuilder = new StatementBuilder();
  57. $statementBuilder->Select('Id, BrowserName, MajorVersion, MinorVersion')
  58. ->From('Browser')
  59. ->OrderBy('BrowserName ASC')
  60. ->Limit(StatementBuilder::SUGGESTED_PAGE_LIMIT);
  61. // Default for result sets.
  62. $resultSet = null;
  63. $combinedResultSet = null;
  64. $i = 0;
  65. do {
  66. // Get all browsers.
  67. $resultSet = $pqlService->select($statementBuilder->ToStatement());
  68. // Combine result sets with previous ones.
  69. $combinedResultSet = (!isset($combinedResultSet))
  70. ? $resultSet
  71. : Pql::CombineResultSets($combinedResultSet, $resultSet);
  72. printf("%d) %d browsers beginning at offset %d were found.\n", $i++,
  73. isset($resultSet->rows) ? count($resultSet->rows) : 0,
  74. $statementBuilder->GetOffset());
  75. $statementBuilder->IncreaseOffsetBy(StatementBuilder::SUGGESTED_PAGE_LIMIT);
  76. } while (isset($resultSet->rows) && count($resultSet->rows) > 0);
  77. // Change to your file location.
  78. $filePath = sprintf("%s/Browsers-%s.csv", sys_get_temp_dir(), uniqid());
  79. $fp = fopen($filePath, 'w');
  80. // Write the result set to a CSV.
  81. fputcsv($fp, Pql::GetColumnLabels($combinedResultSet));
  82. foreach ($combinedResultSet->rows as $row) {
  83. fputcsv($fp, Pql::GetRowStringValues($row));
  84. }
  85. fclose($fp);
  86. printf("Browsers saved to %s\n", $filePath);
  87. } catch (OAuth2Exception $e) {
  88. ExampleUtils::CheckForOAuth2Errors($e);
  89. } catch (ValidationException $e) {
  90. ExampleUtils::CheckForOAuth2Errors($e);
  91. } catch (Exception $e) {
  92. printf("%s\n", $e->getMessage());
  93. }