我有一段簡單的代碼,將來自谷歌發佈請求的數據流作爲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()。 –