2

I am using the php post request example given on http://code.google.com/apis/chart/docs/post_requests.html for generating chart.

The code: 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'> 

Right now when i access another_page.html, the image doesn't load when i click on view image it shows

The image “http://localhost/demo/chartserver-image.php” cannot be displayed, because it contains errors.

What is the issue i am unable to understand?

Please help me on this

Thanks

4 Answers 4

2

Replacing 'content' => http_build_query($chart)))); with 'content' => http_build_query($chart,'', '&')))); resolves the issue.

I have added arg separator '&' to http_build_query() which avoid bug if the arg_separator.output parameter is modified in php.ini.

When i checked phpinfo the arg_separator.output was &amp. That was causing issue so adding '&' to http_build_query() resolves the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 that seems to have solved my problem at stackoverflow.com/questions/3682622/…
oops, sorry, no it didn't -something else did, sorry. But it's a good point to make anyway
1

This will work Absolutely fine... Some mistakes in the above example i think.. they are solved below.

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

Comments

0

This works for me:

<?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 Comments

Thanks for replying can you please clarify the need for 'proxy' => 'tcp://X.X.X.X:XXXX'. What is the value should i pass to this variable? Right now i am getting this error Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\xampp\htdocs\demo\chartserver-image.php on line 33
Warning: fopen(chart.apis.google.com/…; in E:\xampp\htdocs\demo\chartserver-image.php on line 33 Warning: fpassthru(): supplied argument is not a valid stream resource in E:\xampp\htdocs\demo\chartserver-image.php on line 34
I tested it behind a proxy so that's why I had to have the proxy set. I'm no expert in PHP, but it worked for me.
0

I had the identical problem, and I split

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

into two statements:

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

and that seemed to solve it (please don't ask me why)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.