Google Chart Tool示例无效

时间:2010-06-17 11:53:55

标签: php google-visualization

我使用http://code.google.com/apis/chart/docs/post_requests.html上给出的php post请求示例来生成图表。

代码: chartserver-image.php

<?php
 // Create some random text-encoded data for a line chart.
 header('content-type: image/png');
 $url = 'http://chart.apis.google.com/chart';
 $chd = 't:';
 for ($i = 0; $i < 150; ++$i) {
 $data = rand(0, 100000);
 $chd .= $data . ',';
 }
 $chd = substr($chd, 0, -1);

// Add data, chart type, chart size, and scale to params.
$chart = array(
'cht' => 'lc',
'chs' => '600x200',
'chds' => '0,100000',
'chd' => $chd);

// Send the request, and print out the returned bytes.
$context = stream_context_create(
array('http' => array(
  'method' => 'POST',
  'content' => http_build_query($chart))));
fpassthru(fopen($url, 'r', false, $context));
?>

another_page.html

<img width='600' height='200' src='chartserver-image.php'>

现在当我访问another_page.html时,当我点击它显示的视图图像时,图片无法加载

无法显示图像“http://localhost/demo/chartserver-image.php”,因为它包含错误。

我无法理解的问题是什么?

请帮我解决这个问题

由于

4 个答案:

答案 0 :(得分:2)

更换'content'=&gt; http_build_query($图表)))); 'content'=&gt; http_build_query($ chart,'','&amp;'))));解决了这个问题。

我添加了arg separator'&amp;'到http_build_query(),如果有的话可以避免bug arg_separator.output参数在php.ini中修改。

当我检查phpinfo时,arg_separator.output是&amp; amp。这导致了问题所以添加'&amp;'到http_build_query()解决了这个问题。

答案 1 :(得分:1)

这样可以正常工作......上面的例子我认为有些错误......下面就解决了。

<?php

 // Create some random text-encoded data for a line chart.
 header('content-type: image/png');
 $url = 'http://chart.apis.google.com/chart';
 $chd = 't:';
 for ($i = 0; $i < 150; ++$i) {
 $data = rand(0, 100000);
 $chd .= $data . ',';
 }
 $chd = substr($chd, 0, -1);

// Add data, chart type, chart size, and scale to params.
$chart = array(
'cht' => 'lc',
'chs' => '600x200',
'chds' => '0,100000',
'chd' => $chd);

$query = http_build_query($chart);

$fullurl = $url."?".$query;

$context = stream_context_create(

    array(
            'http' => array(
                    'method' => 'GET',
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                                "Content-length: 0",

            )
    )
);

$ret = fopen($fullurl, 'r', false, $context);
fpassthru($ret);
?>

答案 2 :(得分:0)

这对我有用:

<?php

 // Create some random text-encoded data for a line chart.
 //header('content-type: image/png');
 $url = 'http://chart.apis.google.com/chart';
 $chd = 't:';
 for ($i = 0; $i < 150; ++$i) {
 $data = rand(0, 100000);
 $chd .= $data . ',';
 }
 $chd = substr($chd, 0, -1);

// Add data, chart type, chart size, and scale to params.
$chart = array(
'cht' => 'lc',
'chs' => '600x200',
'chds' => '0,100000',
'chd' => $chd);

$query = http_build_query($chart);

$fullurl = $url."?".$query;

$context = stream_context_create(

    array(
            'http' => array(
                    'method' => 'GET',
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                                "Content-length: 0",
                    'proxy' => 'tcp://X.X.X.X:XXXX'
            )
    )
);

$ret = fopen(fullurl, 'r', false, $context);
fpassthru($ret);

答案 3 :(得分:0)

我有同样的问题,我分手

$context = stream_context_create(
array('http' => array(
  'method' => 'POST',
  'content' => http_build_query($chart))));

分为两个陈述:

$x = array('http' => array(
  'method' => 'POST',
  'content' => http_build_query($chart)));

$context = stream_context_create($x);

这似乎解决了它(请不要问我原因)