我无法返回执行echo或print_r的json文件。这是我的代码:
<?php
include 'logFolder.php';
$email = "48k2r4l6o60nbn2cug9p0luk7c@group.calendar.google.com";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
//echo $url;
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$i = 0;
$ns=$feed->getNameSpaces(true);
$text = '[';
foreach ($feed->entry as $entry) {
$when=$entry->children($ns["gd"]);
$when_atr=$when->when[0]->attributes();
$start=$when_atr['startTime'];
$end=$when_atr['endTime'];
$url = $entry->link->attributes();
$url = $url['href'];
$title=addslashes($entry->title);
if($i > 0){
$text = $text . ',';
}
$text = $text . '{"date":"'.$start.'","type":"meeting","title":"'.$title.'","description":" ","url":"'.$url.'"}';
// $text[$i] = array("date"=>$start, "type"=>"meeting", "title"=>$title, "description"=>" ", "url"=>$url);
$File = $logFolder . 'ajson' . '.json' ;
$fh = fopen($File, 'a') or die();
fwrite($fh, '{"date":"'.$start.'","type":"meeting","title":"'.$title.'","description":" ","url":"'.$url.'"}');
fclose($fh);
}
$text = $text . ']';
echo $text;
?>
我正在阅读谷歌日历中的事件,我想在我的json文件中包含一些信息。我可以将其保存到文件中,您可以在我的代码中看到,因此变量具有正确的内容。但是,每次我想在屏幕上打印它或以回声方式返回时,我都没有打印出来。你知道发生了什么吗?
答案 0 :(得分:0)
使用数组和json_encode();
include 'logFolder.php';
$email = "48k2r4l6o60nbn2cug9p0luk7c@group.calendar.google.com";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
//echo $url;
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$i = 0;
$ns=$feed->getNameSpaces(true);
$data = array();
foreach ($feed->entry as $entry) {
$when=$entry->children($ns["gd"]);
$when_atr=$when->when[0]->attributes();
$start=$when_atr['startTime'];
$end=$when_atr['endTime'];
$url = $entry->link->attributes();
$url = $url['href'];
$title=addslashes($entry->title);
$data[] = array( 'date' => $start, 'type' => 'meeting', 'title' => $title, 'description', 'url' => $url);
}
$json = json_encode($data);
$File = $logFolder . 'ajson' . '.json' ;
$fh = fopen($File, 'a') or die();
fwrite($fh,$json);
fclose($fh);
echo $json;
答案 1 :(得分:0)
你应该使用json_encode。
在你的代码中,这就像是将每个条目单独保存为文件并输出总json(如上面的代码所示)...
<?php
include 'logFolder.php';
$email = "48k2r4l6o60nbn2cug9p0luk7c@group.calendar.google.com";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$ns=$feed->getNameSpaces(true);
//init new array
$jsonArray=array();
foreach ($feed->entry as $entry) {
$when=$entry->children($ns["gd"]);
$when_atr=$when->when[0]->attributes();
$start=$when_atr['startTime'];
$end=$when_atr['endTime'];
$url = $entry->link->attributes();
$url = $url['href'];
$title=addslashes($entry->title);
//create local json array
$thisJson=array(
'date'=>$start,
'type'=>'meeting',
'title'=>$title,
'description'=> ' ' ,
'url' => $url
);
//add local json array to big json array
$jsonArray[]=$thisJson
$File = $logFolder . 'ajson' . '.json' ;
$fh = fopen($File, 'a') or die();
//save json this json array as text
fwrite($fh, json_encode($thisJson));
fclose($fh);
}
//output compiled json array
echo(json_encode($jsonArray));
?>
在otice中有一件事是你的fopen每次使用smae文件名,所以它只是一遍又一遍地覆盖自己。不知道你为什么要这样做,但如果你想要一个包含所有json的文件,你需要在循环之后这样做...
<?php
include 'logFolder.php';
$email = "48k2r4l6o60nbn2cug9p0luk7c@group.calendar.google.com";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$ns=$feed->getNameSpaces(true);
//init new array
$jsonArray=array();
foreach ($feed->entry as $entry) {
$when=$entry->children($ns["gd"]);
$when_atr=$when->when[0]->attributes();
$start=$when_atr['startTime'];
$end=$when_atr['endTime'];
$url = $entry->link->attributes();
$url = $url['href'];
$title=addslashes($entry->title);
//create local json array
$thisJson=array(
'date'=>$start,
'type'=>'meeting',
'title'=>$title,
'description'=> ' ' ,
'url' => $url
);
//add local json array to big json array
$jsonArray[]=$thisJson
}
//output compiled json array
echo(json_encode($jsonArray));
//save the json
$File = $logFolder . 'ajson' . '.json' ;
$fh = fopen($File, 'a') or die();
//save json this json array as text
fwrite($fh, json_encode($jsonArray));
fclose($fh);
?>
答案 2 :(得分:0)
你有一个简单的错误 - $i
永远不会增加,所以你在生成的输出中缺少“,”因此它是非法的json语法。
在foreach
循环中,添加:
$i++;
循环结束前。
但我也同意使用json_encode()
更好,但更慢
答案 3 :(得分:0)
function getStatusCodeMessage($status)
{
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
function sendResponse($status = 200, $body = '', $content_type = 'text/json')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
尝试在返回json时使用这些函数我将在localhost中创建json的可下载文件