PHP脚本会导致array_merge和usort警告。结果文件为空
我对这件事很困惑。我的服务器上有一个PHP文件。它的编写目的是从Google日历中提取事件信息,并将结果数据写入单独的文本文件中。PHP文件在7月1日之前一直运行良好。我不确定是什么导致了这些错误,但同一个脚本正被另一个人使用(他写的,而且对他来说仍然很好),所以我很确定这不是谷歌改变的结果。以下是访问文件时返回的特定错误 `警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData/DataSources/index.php中的数组 警告:usort()[function.usort]:参数应该是第120行/home/westfork/public_html/AppData/DataSources/index.php中的数组` 这是PHP文件。任何帮助都会很好PHP脚本会导致array_merge和usort警告。结果文件为空,php,Php,我对这件事很困惑。我的服务器上有一个PHP文件。它的编写目的是从Google日历中提取事件信息,并将结果数据写入单独的文本文件中。PHP文件在7月1日之前一直运行良好。我不确定是什么导致了这些错误,但同一个脚本正被另一个人使用(他写的,而且对他来说仍然很好),所以我很确定这不是谷歌改变的结果。以下是访问文件时返回的特定错误 `警告:array#u merge()[function.array merge]:参数#2不是第111行/home/westfork/public_html/AppData
<?php
$today = date("F j, Y");
$thisWeek = date("W");
$thisYear = date("Y");
/*
* Represents individual events within a calendar
*/
Class Event
{
public $allDay; // "1" or ""
public $multiDay; // "1" or ""
public $date; // "F j, Y"
public $time; // String
public $start; // Timestamp
public $end; // Timestamp
public $section; // String
public $title; // String
public $description; // String
public $location; // String
/**
* @param array $event - an entry node from the Google calendar feed
*/
public function __construct($event)
{
// set the Event properties based on the array keys and values
foreach($event as $key=>$value) {
$this->{$key} = $value;
}
$this->time = $this->relativeTime();
// set the section header for this event
$this->section = $this->sectionHeader();
}
/**
* @return string - the section heading based on when the event is in relation to the
* current date, e.g., "Today", "This Week", "May", "Dec", etc.
*/
protected function sectionHeader() {
global $today, $thisWeek, $thisYear;
// determine which section header to apply
if (strtotime($today) == $this->date) {
return "Today";
} else if (date("F j, Y", strtotime($today) + (60*60*24)) === date("F j, Y", $this->date)) {
return "Tomorrow";
} else if ($thisYear === date("Y", $this->date)) {
return date("l, F j", $this->date);
} else {
return date("l, F j, Y", $this->date);
}
}
protected function relativeTime() {
// if the event isn't and all-day event, set the relative start and end times
if (!$this->allDay) {
// if the start and end times are outside of the current day, set $allDay to "1"
if ($this->start <= $this->date && $this->end >= $this->date + (60*60*24)) {
return "All Day";
}
// if both the start and end are within the current day
else if ($this->start > $this->date && $this->end < $this->date + (60*60*24)) {
return date("g:i A", $this->start) . " - " . date("g:i A", $this->end);
}
// if the start is within the current day
else if ($this->start > $this->date && $this->start < $this->date + (60*60*24)) {
return date("g:i A", $this->start);
}
// if the end is within the current day
else if ($this->end > $this->date && $this->end < $this->date + (60*60*24)) {
return 'ends: ' . date("g:i A", $this->start);
}
} else {
return "All Day";
}
}
}
/*
* Represents a calendar containing events
*/
Class Calendar
{
protected $events = array();
protected $name;
/**
* @param String $name - the name of the calendar (will be used as the file name for saving)
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Adds a an array of Event to the calendar.
* @param array $events - an array of Events
*/
public function addEvents($events)
{
$this->events = array_merge($this->events, $events);
$this->sortEvents();
}
/**
* Sorts the $this->events array
*/
protected function sortEvents()
{
usort($this->events, array($this, 'compareTwoEventsForSort'));
}
/**
* Compare function for sortEvents()
* Compares first by Event::$date and then Event::$start
*/
protected function compareTwoEventsForSort($x, $y) {
// compare the dates
if ( $x->date < $y->date) {
return -1;
}
if ( $x->date > $y->date) {
return 1;
}
// the dates are equal, so now compare the times
if ($x->start < $y->start) {
return -1;
} else if ($x->start > $y->start) {
return 1;
} else {
return 0;
}
}
public function listEvents() {
print_r($this->events);
}
public function saveCalendarAsFile() {
// Log the results into separate calendar text files.
$file = fopen("{$this->name}.txt", 'w');
fwrite($file, json_encode($this->events));
fclose($file);
}
}
/*
* Handles the creation and saving of Calendars and Events
*/
Class CalendarController
{
public $calendar;
/**
* @param String $id - the id of the google calendar
*/
public function __construct($name, $id)
{
$today = date("Y-m-d");
if ( strtotime(date("Y") . "-06-01") > time()) {
$yearEnd = date("Y") . "-08-01";
} else {
$yearEnd = ((int) date("Y") + 1) . "-08-01";
}
$url = "https://www.google.com/calendar/feeds/{$id}/public/";
$url .= "full?";
$url .= "&start-min=$today&start-max=$yearEnd";
$url .= "&max-results=1000";
$url .= "&orderby=starttime";
$url .= "&sortorder=a";
$url .= "&singleevents=true";
$url .= "&ctz=America/Chicago";
$url .= "&fields=entry(title,content,gd:where,gd:when)";
$xml = file_get_contents($url);
// get rid of the namespace "gd:"
$xml = str_replace("gd:", "", $xml);
$xml = new SimpleXMLElement($xml);
$events = $this->parseEventsFromGoogleXML($xml);
$this->calendar = new Calendar($name);
$this->calendar->addEvents($events);
}
public function listCalendarEvents() {
$this->calendar->listEvents();
}
public function saveCalendarAsFile() {
$this->calendar->saveCalendarAsFile();
}
/**
* @param SimpleXMLElement $xml - a simpleXML object of the Google calendar feed
*/
protected function parseEventsFromGoogleXML($xml)
{
global $today;
$events = array();
foreach ($xml->entry as $entry) {
$start = strtotime($entry->when['startTime']);
$end = strtotime($entry->when['endTime']);
// if the $end time is 12:00 AM the next morning, make it 11:59 PM the previous day
if (date("H:i", $end) === "00:00") {
$end -= 1;
}
$startDate = date("F j, Y", $start);
$endDate = date("F j, Y", $end);
$allDay = (strpos($entry->when['startTime'] . $entry->when['endTime'], "T") === false) ? "1" : "";
$multiDay = ($startDate !== $endDate) ? "1" : "";
// iterate through each day of the event
$loopEndDate = strtotime($endDate);
$loopToday = strtotime($today);
$loopDate = strtotime($startDate);
for ( ; $loopDate <= $loopEndDate; $loopDate += (60*60*24) )
{
if ($loopDate >= $loopToday) {
// clean up the description text
// replace <br> tags with \n, delete \t characters, and remove all non-link tags
$description = (string) $entry->content;
$description = trim($description);
$description = preg_replace("/<br[^>]*>/i", "\n", $description);
$description = preg_replace("/\n */", "\n", $description);
$description = preg_replace("/\n{3,}/", "\n\n", $description);
$description = strip_tags($description, "<a>");
$description = preg_replace("/([ \t]*$)|(^[ \t]*)/m", "", $description);
$description = htmlspecialchars($description, ENT_NOQUOTES, 'UTF-8', false);
$event = new Event(array(
'date' => $loopDate,
'allDay' => $allDay,
'multiDay' => $multiDay,
'start' => $start,
'end' => $end,
'title' => (string) $entry->title,
'description' => $description,
'location' => (string) $entry->where['valueString'],
));
$events[] = $event;
}
}
}
return $events;
}
}
$calendars = array(
"Academic" => "westmont.edu_tker7t4jgfoi6i7msien0smimk@group.calendar.google.com",
"Athletics" => "westmont.edu_a2o3k05co4i3tt5voc1ginging@group.calendar.google.com",
"Chapel" => "westmont.edu_olec4f6vb91cn02e9higcab57c@group.calendar.google.com",
"Events" => "westmont.edu_mnjvcd9e9224q9m723nurbl8ks@group.calendar.google.com",
"Dining" => "3mipcafj6qeqs1m9mfpngv97q0@group.calendar.google.com",
"Band" => "westforkband.org_kg9n1kin34c3eovt6l2gomvlb8%40group.calendar.google.com",
"Rehearsal" => "westforkband.org_mvibrgiu71ldvrnfh6r4rdf3fc@group.calendar.google.com",
"Parking" => "westforkband.org_t8jj4b5l9b3li2n31ce67ecc84@group.calendar.google.com",
// "Development" => "westmont.edu_atsog3h7vsf4k3s5r1f6e2p660%40group.calendar.google.com",
);
foreach($calendars as $name=>$id) {
$ecc = new CalendarController($name, $id);
$ecc->listCalendarEvents();
$ecc->saveCalendarAsFile();
}
您是否尝试过在addEvents
方法中输出$events
参数中的内容
尝试执行var\u转储($events)代码>在第111行之前,查看将要输入的内容
var_dump($events);
$this->events = array_merge($this->events, $events);
这将显示$events
的完整结构,包括属性类型。如果这是通过cron作业或其他方式运行的,您可能希望将var输出到日志文件或其他方式
此外,在尝试合并之前,您最好检查$events
是否为数组:
if (is_array($events)) {
$this->events = array_merge($this->events, $events);
$this->sortEvents();
}
如果你每次都应该是一个数组,你仍然应该保留这个条件,但是添加一个else
,通过日志通知你$events
是什么,这样你就可以在这些情况下正确地处理它。我不想这么说,但是我粘贴了提供的代码,它对我有效(感谢给定的日历地址)parseEventsFromGoogleXML
必须失败,可能是因为连接错误、时间限制或其他原因。你需要处理这个问题。将$events
设置为$events=$this->parseEventsFromGoogleXML($xml)之前的空数组代码>,使用异常并尝试/捕获它们,在继续之前检查连接和/或XML结果,等等。确保您的服务器可以从外部处理文件。抱歉,无法复制。至少灯不亮5.3.5请将代码精简到最基本的部分。那些渴望获得分数的人可能会投入努力,那些不太愿意的人可能会投票结束你的问题。我们非常喜欢简单的输入->最小代码->输出。在这样做的过程中,你甚至可以自己发现错误。大家好。对此很抱歉,但这些问题都无法重现的原因是,我给您的代码中显然不存在这些问题。出于某种原因,我对文件所做的任何更改(包括删除)都会反映在基于web的文件管理器中,但实际上并未应用。我不得不使用FTP来进行任何永久性的文件更改。我不确定是怎么回事,但问题已经解决了。非常感谢你们给予的帮助。很抱歉占用了你的时间。