PHP打开flash图表2错误(摆脱它)

时间:2012-02-20 04:52:55

标签: php error-handling charts

我是这个论坛的新用户并打开Flash Chart 2.我刚在我的一个网站上安装了一个图表,工作正常。我使用PHP中的file_get_contents通过URL动态获取图表点。一切正常,但有些网址没有点数据,因此图表会返回JSON错误

Open Flash Chart

JSON Parse Error [Syntax Error]
Error at character 0, line 1:

0: <br />

我知道为什么这个错误被打了,但我想摆脱这个,相反它应该显示“没有足够的数据来映射图表”。

有没有解决方案?

提前谢谢你:D

编辑: 我的代码:

<?php
include 'oc/php-ofc-library/open-flash-chart.php';
$url = 'http://*****.api3.nextbigsound.com/metrics/artist/'.$_REQUEST['artistId'].'.json';
$file = file_get_contents($url);

$data = json_decode($file);

foreach($data as $Idx => $key){
    if($key->Service->name === 'MySpace'){

        foreach($key->Metric as $Index => $Item){
            $ctr = 1;
            if($Index == "views"){
                foreach($Item as $a => $b){
                    $record[] = $b;
                }
            }

        }

    }

}

$year = array();
$price = array();
$year[] = 'Sun, 12 Feb 2012'; $price[] = 36.7;
$year[] = 'Sun, 13 Feb 2012'; $price[] = 38.7;
$year[] = 'Sun, 14 Feb 2012'; $price[] = 42.8;
$year[] = 'Sun, 15 Feb 2012'; $price[] = 38.2;
$year[] = 'Sun, 16 Feb 2012'; $price[] = 37.8;
$year[] = 'Sun, 17 Feb 2012'; $price[] = 34.7;
$year[] = 'Sun, 18 Feb 2012'; $price[] = 38.4;



$chart = new open_flash_chart();

$chart->set_bg_colour( '#FFFFFF' );

$title = new title( 'MySpace Views' );
$title->set_style( "{font-size: 20px; background:#fff; color: #A2ACBA; text-align: center; width: 300px; border-radius:50px;}" );
$chart->set_title( $title );

$area = new area();
$area->set_colour( '#5B56B6' );
$area->set_values( $record );
$area->set_key( 'Views', 7 );
$chart->add_element( $area );

$x_labels = new x_axis_labels();
$x_labels->set_steps( 1 );
//$x_labels->set_vertical();
$x_labels->set_colour( '#A2ACBA' );
$x_labels->set_labels( $year );

$x = new x_axis();
$x->set_colour( '#A2ACBA' );
$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps(1);
$x->set_range('0','6');
// Add the X Axis Labels to the X Axis
$x->set_labels( $x_labels );

$chart->set_x_axis( $x );

//
// LOOK:
//
$x_legend = new x_legend( '2012' );
$x_legend->set_style( '{font-size: 20px; color: #778877}' );
$chart->set_x_legend( $x_legend );

//
// remove this when the Y Axis is smarter
//
$y = new y_axis();
$min = min($record);
$max = max($record);
$len = strlen($max);
if($len == '9'){
    $diff = '10000';
}elseif($len == '8'){
    $diff = '1000';
}elseif($len == '7'){
    $diff = '1000';
}elseif($len == '6'){
    $diff = '1000';
}elseif($len == '5'){
    $diff = '1000';
}

$y->set_range( $min, $max, $diff );
$chart->add_y_axis( $y );

echo $chart->toPrettyString();

以下是print_r($data)

Array ( [0] => stdClass Object ( [Service] => stdClass Object ( [name] => MySpace [id] => 1 ) [Profile] => stdClass Object ( [url] => http://www.myspace.com/adamgreen1 [id] => 384 ) [Metric] => Array ( ) ) [1] => stdClass Object ( [Service] => stdClass Object ( [name] => Last.fm [id] => 2 ) [Profile] => stdClass Object ( [url] => http://www.last.fm/music/adam+green [id] => 174985 ) [Metric] => stdClass Object ( [plays] => stdClass Object ( [15383] => 10165386 [15384] => 10165386 [15385] => 10165375 [15386] => 10168408 [15387] => 10171611 [15388] => 10174725 [15389] => 10177797 ) [fans] => stdClass Object ( [15383] => 242392 [15384] => 242392 [15385] => 242535 [15386] => 242580 [15387] => 242641 [15388] => 242709 [15389] => 242775 ) [comments] => stdClass Object ( [15383] => 916 [15384] => 916 [15385] => 918 [15386] => 918 [15387] => 918 [15388] => 918 [15389] => 918 ) ) ) [2] => stdClass Object ( [Service] => stdClass Object ( [name] => Wikipedia [id] => 17 ) [Profile] => stdClass Object ( [url] => http://en.wikipedia.org/wiki/Adam_Green_(musician) [id] => 918802 ) [Metric] => Array ( ) ) ) 

2 个答案:

答案 0 :(得分:0)

鉴于该错误,似乎您的PHP脚本在某些情况下返回HTML而不是json。这就是你必须要修复的地方。如果你的Flash东西需要 ONLY json字符串,那么你就不能允许从脚本中输出任何JUT字符串。

答案 1 :(得分:0)

运行为html提供数据的php并检查语法错误