PHP打开flash图表2错误(去掉它)


PHP open flash chart 2 error(get rid of it)

我是这个论坛和Open Flash Chart 2的新手。我刚刚在我的一个网站上安装了一个图表,效果很好。我使用PHP中的file_get_contents通过URL动态获取图表点。一切都很好,但有些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 ( ) ) ) 

考虑到这个错误,在某些情况下,php脚本似乎返回了HTML,而不是json。这就是你必须解决的问题。如果你的Flash程序需要ONLYjson字符串,那么除了json字符串之外,你永远不允许从脚本中输出任何东西。

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