2010-06-17 55 views
2

我正在使用在http://code.google.com/apis/chart/docs/post_requests.html上給出的PHP發佈請求示例來生成圖表。谷歌圖表工具示例不工作

代碼: 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」無法顯示,因爲它包含錯誤。

我無法理解的問題是什麼?

請幫我在這

感謝

回答

2

更換 '內容'=> http_build_query($圖)))); 'content'=> http_build_query($ chart,'','&'))));解決了這個問題。

我已經添加精氨酸分離器「&」到http_build_query(),它避免錯誤如果 arg_separator.output參數在php.ini被修改。

當我檢查phpinfo的arg_separator.output是& amp。這是一個問題,因此在http_build_query()中添加'&'可以解決問題。

+0

+1似乎已經解決了我的問題在http://stackoverflow.com/questions/3682622/anyone-having-problems-with-google-charts-in-xampp – Mawg 2010-09-10 08:08:51

+0

哎呀,對不起,不,它沒有 - 一些東西別的,對不起。但無論如何,這是一個好點 – Mawg 2010-09-10 10:22:23

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); 
+0

感謝您的回覆,請您澄清'proxy'=>'tcp://X.X.X.X:XXXX'的必要性。我應該傳遞給這個變量的值是多少? 現在我得到這個錯誤 警告:fopen()[function.fopen]:php_network_getaddresses:getaddrinfo失敗:沒有這樣的主機是已知的。在線33:E:\ xampp \ htdocs \ demo \ chartserver-image.php – 2010-06-18 05:42:56

+0

警告:fopen(http://chart.apis.google.com/chart?cht=lc & chs = 600x200 & chds = 0%2C100000 & in E:\ xampp \ htdocs \ demo \ chartserver-image.php on line 33 警告:fpassthru():提供的參數不是E:\ xampp \ htdocs \ demo \ chartserver-image.php中的有效流資源在線34 – 2010-06-18 05:43:48

+0

我在代理後面對它進行了測試,這就是爲什麼我必須擁有'proxy'集合。我不是'PHP'的專家,但它對我很有幫助。 – 2010-06-19 17:20:28

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); 

而且似乎解決它(請不要問我爲什麼)

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); 
?>