2011-06-16 181 views
0

我试图获得Google Adwords API,并且开箱即用,有一个示例目录可以从命令行正常运行,但当我离开时它不能正常运行在浏览器中它只是读取:PHP脚本从命令行运行,但没有在浏览器中运行

错误324(net :: ERR_EMPTY_RESPONSE):服务器关闭连接而不发送任何数据。

下面是全部代码:

<?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 <[email protected]> 
*/ 

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(); 
} 

我能做些什么来解决这个问题呢?

谢谢!

+0

也许脚本会做它应该做的任何事情,但根本不会发送任何数据?在源代码中查看,这是可能的 – 2011-06-16 22:31:06

+0

printf只打印在cli中而不是在浏览器中? – 2011-06-16 22:32:42

+1

@Pete nope,printf总是输出,但可能有一个情况,它永远不会被触发。尝试一个无条件的'回声“确定”'看看会发生什么 – 2011-06-16 22:34:34

回答

2

那么,它看起来很明智。调试PHP可能是一件痛苦的事情,通常没有什么比做更好的事情要做的事情要比添加一个echo s或error_log s的负载,并且通过代码找到导致问题的线路。在顶部重复一遍以确保正确解析,等等。这个问题要么在代码中,要么在配置中,你可能不得不自己去纠缠一些,直到你能从中获得更多的输出。

+0

嗯感谢您的意见,我发现这条线是问题的原因(从注释每一件事和工作后台:$ page = $ targetingIdeaService-> get($ selector); – 2011-06-16 22:38:08

+0

是否有API的文档?'$ selector'的值是否看起来像应该被传入的字符串吗?你可以在调用之后调用'$ page'的print_r'值吗? – 2011-06-16 22:50:44

+0

我实际上只是尝试过,我print_r'd并在后面注释掉整个if/else,并且它仍然不会在浏览器中打印,当我尝试在cli中再次运行它时,它实际上会打印出包含adwords沙盒中的多个关键字的整个响应,$ selector在浏览器和cli中显示完全相同 – 2011-06-16 22:52:16

相关问题