Browser 从命令行运行但不在浏览器中的PHP脚本
我正在尝试获取Google Adwords API,其中有一个示例目录可以从命令行正常运行,但当我在浏览器中打开时,它无法正常工作,它只显示: 错误324(net::ERR_EMPTY_RESPONSE):服务器关闭了连接,但未发送任何数据 以下是完整的代码:Browser 从命令行运行但不在浏览器中的PHP脚本,browser,php,Browser,Php,我正在尝试获取Google Adwords API,其中有一个示例目录可以从命令行正常运行,但当我在浏览器中打开时,它无法正常工作,它只显示: 错误324(net::ERR_EMPTY_RESPONSE):服务器关闭了连接,但未发送任何数据 以下是完整的代码: <?php /** * This example gets keywords related to a seed keyword. * * Tags: TargetingIdeaService.get * Restricti
<?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();
}
嗯,这看起来很合理。调试PHP可能是一件痛苦的事情,通常没有什么比添加
echo
s或error\u log
s的负载并在代码中平分找到导致问题的行更好的方法。在顶部敲入一个回音,以确保它被正确解析,以此类推。问题要么在代码中,要么在配置中,您可能不得不进行一些修补,直到您可以从中获得更多的输出。脚本可能执行它应该执行的任何操作,但根本不发送任何数据?粗略地看一下源代码,有可能只在cli中打印printf,而不在浏览器中打印?@Pete-nope,printf总是输出,但可能会出现从未触发的情况。尝试无条件的回显“OK”
以查看发生了什么;除非我注释掉$page=$targetingdiaservice->get($selector),否则不会打印@Pete在这种情况下,PHP很可能会崩溃并显示错误消息。也许display\u errors
没有打开?嗯,谢谢你的建议,我发现这一行是问题的原因(从注释每一件事和使用backwords:$page=$targetingdiaservice->get($selector)有API的文档吗?$selector
的值看起来像应该传入的字符串吗?你能在调用后打印$page
的值吗?等等?我实际上刚刚试过,我打印了它,然后注释掉了整个if/else,当我在浏览器中输入时,它仍然不能打印如果要在cli中再次运行它,它实际上会打印出包含adwords沙盒中多个关键字的整个响应,$selector在浏览器和cli中的显示完全相同。它使用“auth.ini”文件为其提供凭据,这会产生影响吗?