我有一段简单的代码,将来自谷歌发布请求的数据流作为PNG输出。这是为了使用谷歌创建一个QRcode。我想要做的是将此文件保存为我的服务器上的PNG文件,我似乎无法弄清楚如何处理它,因为我不熟悉使用流。下面的代码:通过Google POST请求保存PNG
<?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));
?>
是否必须是帖子?生成的url作为一个简单的GET请求正常工作,这意味着您可以使用'echo file_get_contents(...)'代替。 –
它可以是一个获取请求,但仍然不确定我将如何保存它。 http://code.google.com/apis/chart/infographics/docs/overview.html – Throttlehead
'file_put_contents('qr.png',file_get_contents(...));'fpassthru()用于直接发送输出到客户。对于你的代码,你需要在之前打开的文件句柄上使用fwrite()。 –