I have a simple bit of code that outputs the stream from a google post request as a PNG. It’s for using google to create a QRcode. What I want to do though is save this as a PNG file on my server and I can’t seem to figure out how to go about it as I’m not so familiar with working with streams. Here’s the code:
<?php
//This script will generate the slug ID and create a QRCode by requesting it from Google Chart API
header('content-type: image/png');
$url = 'https://chart.googleapis.com/chart?';
$chs = 'chs=150x150';
$cht = 'cht=qr';
$chl = 'chl='.urlencode('Hello World!');
$qstring = $url ."&". $chs ."&". $cht ."&". $chl;
// Send the request, and print out the returned bytes.
$context = stream_context_create(
array('http' => array(
'method' => 'POST',
'content' => $qstring
)));
fpassthru(fopen($url, 'r', false, $context));
?>
This is one way, based on your code and specified ‘save this as a PNG file on my server’:
Add error checking etc. to taste.