如何将查询字符串附加到对象?

时间:2011-03-20 06:54:11

标签: php oop api zend-framework

谷歌在日历API方面遇到了一些问题,现在我需要修复一些将自己添加到文本中的奇怪符号。我发现这个线程正好描述了我正在处理的问题: http://www.google.com/support/forum/p/Calendar/thread?tid=25ac3d762b235a51&hl=en

解决方案是将“& hl = en”或“?hl = en”附加到我的“基本”网址Feed的末尾。

我很高兴怎么做,因为我正在使用Zend_Gdata检索Feed作为对象:

<?php
// load library
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Http_Client');

// create authenticated HTTP client for Calendar service
$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "xxxxx";
$pass = "xxxxx";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
$gcal = new Zend_Gdata_Calendar($client);

$query = $gcal->newEventQuery();
$query->setUser('xxxxx@group.calendar.google.com');
$secondary=true;
$query->setVisibility('private');
$query->setProjection('basic');
$query->setOrderby('starttime');
$query->setSortOrder('ascending');
//$query->setFutureevents('true');

$startDate=date('Y-m-d h:i:s');
$endDate="2015-12-31";
$query->setStartMin($startDate);
$query->setStartMax($endDate);
$query->setMaxResults(30);
try {
  $feed = $gcal->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
  echo "Error: " . $e->getResponse();
}

?>

我试着这样做没有运气:

$query->setProjection('basic?hl=en');

1 个答案:

答案 0 :(得分:1)

注意:我之前没有使用任何此类内容,所以如果这不起作用我会道歉:)

documentation表示getCalendarEventFeed()可以将网址作为参数。 所以我们可以改变这个......

$feed = $gcal->getCalendarEventFeed($query);

...以便将参数添加到查询字符串中:

$eventUrl = $query->getQueryUrl() . '?hl=en';
$feed = $gcal->getCalendarEventFeed($eventUrl);

显然,这是一个简化的示例 - 您仍应执行以下两项检查:

  1. 在致电$query之前,请确保Zend_Gdata_QuerygetQueryUrl()的实例。

  2. 确保没有其他查询参数已添加到$eventUrl,以便我们可以使用&hl=en而不是?hl=en。您可以使用Zend_Uri进行此操作。