简体   繁体   English

PHP脚本从命令行运行但不在浏览器中运行

[英]PHP Script running from command line but not in browser

I am trying to get the Google Adwords API, and right out of the box there's an example directory that runs fine from the command line but it doesn't work properly when I go to in the browser it just reads: 我正在尝试使用Google Adwords API,并且开箱即用,有一个示例目录可以从命令行运行正常,但是当我在浏览器中看到它时,它无法正常工作:

Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data. 错误324(net :: ERR_EMPTY_RESPONSE):服务器关闭连接而不发送任何数据。

Here is the code in its entirety: 以下是完整的代码:

<?php
/**
 * This example gets keywords related to a seed keyword.
 *
 * Tags: TargetingIdeaService.get
 * Restriction: adwords-only
 *
 * PHP version 5
 *
 * 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    GoogleApiAdsAdWords
 * @subpackage v201101
 * @category   WebServices
 * @copyright  2011, Google Inc. All Rights Reserved.
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License,
 *             Version 2.0
 * @author     Eric Koleda <api.ekoleda@gmail.com>
 */

error_reporting(E_STRICT | E_ALL);

// You can set the include path to src directory or reference
// AdWordsUser.php directly via require_once.
// $path = '/path/to/aw_api_php_lib/src';
$path = dirname(__FILE__) . '/../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'Google/Api/Ads/Common/Util/MapUtils.php';

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.
  $targetingIdeaService = $user->GetTargetingIdeaService('v201101');

  // Create seed keyword.
  $keyword = new Keyword();
  $keyword->text = 'mars cruise';
  $keyword->matchType = 'BROAD';

  // Create selector.
  $selector = new TargetingIdeaSelector();
  $selector->requestType = 'IDEAS';
  $selector->ideaType = 'KEYWORD';
  $selector->requestedAttributeTypes =
      array('CRITERION', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');

  // Set selector paging (required for targeting idea service).
  $paging = new Paging();
  $paging->startIndex = 0;
  $paging->numberResults = 10;
  $selector->paging = $paging;

  // Create related to keyword search parameter.
  $relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter();
  $relatedToKeywordSearchParameter->keywords = array($keyword);

  // Create keyword match type search parameter to ensure unique results.
  $keywordMatchTypeSearchParameter = new KeywordMatchTypeSearchParameter();
  $keywordMatchTypeSearchParameter->keywordMatchTypes = array('BROAD');

  $selector->searchParameters =
      array($relatedToKeywordSearchParameter, $keywordMatchTypeSearchParameter);

  // Get related keywords.
  $page = $targetingIdeaService->get($selector);

  // Display related keywords.
  if (isset($page->entries)) {
    foreach ($page->entries as $targetingIdea) {
      $data = MapUtils::GetMap($targetingIdea->data);
      $keyword = $data['CRITERION']->value;
      $averageMonthlySearches =
          isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
          ? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
      printf("Keyword with text '%s', match type '%s', and average monthly "
          . "search volume '%s' was found.\n", $keyword->text,
          $keyword->matchType, $averageMonthlySearches);
    }
  } else {
    print "No related keywords were found.\n";
  }
} catch (Exception $e) {
  print $e->getMessage();
}

What can I do to troubleshoot this problem? 我该怎么做才能解决这个问题?

Thanks! 谢谢!

Well, it looks sensible. 嗯,看起来很合理。 Debugging PHP can be a pain, and there's often nothing better to do than add a load of echo s or error_log s and bisect your way through the code to find the line that's causing the problem. 调试PHP可能会很麻烦,而且通常没有什么比添加一个echo s或error_log更好的了,并且通过代码将您的方法分成两部分来查找导致问题的行。 Whack in an echo at the top to make sure it's being parsed properly, and so on. 在顶部回声,确保它被正确解析,等等。 The problem's either in the code, or the configuration, and you might have to resign yourself to some tinkering until you can get some more output from it. 问题出在代码或配置中,你可能不得不让自己屈服于一些修补,直到你可以从它获得更多的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM