2017-04-11 24 views
0

我一个试图从URL的结果(图像),但有一些事情出错代码 我的代码是:无法显示合成图像(PHP代码)

<?php 
if(isset($_POST['submit'])) 
{ 
$url= $_POST['url']; 
$qrurl= 'http://www.qr-code-generator.com/phpqrcode/getCode.php? 
cht=qr&chl='.$url.'&chs=180x180&choe=UTF-8&chld=L|0'; 
$result = file_get_contents("$qrurl"); 
echo $result; 
} 
else 
{ 
echo"<form method='post' action='index.php' > 
<input name='url' class='material' placeholder='Enter url...' type='url'autofocus/></br><br> 
<input name='submit' id='submit' type='submit' value='submit'/></br><br> 
</form> 
"; 
} 
?> 

上面的代码中显示任何 但是当我在浏览器中打开url时,它显示qr码。

回答

3

您需要一个(PNG)头文件,因为这是它正在创建的文件的类型,并确保该URL位于一行而不是分成两部分; 非常重要

<?php 
if(isset($_POST['submit'])) 
{ 
$url= $_POST['url']; 
$qrurl= 'http://www.qr-code-generator.com/phpqrcode/getCode.php?cht=qr&chl='.$url.'&chs=180x180&choe=UTF-8&chld=L|0'; 
$result = file_get_contents("$qrurl"); 
echo $result; 

header ('Content-Type: image/png'); 
exit; 

} 
else 
{ 
echo"<form method='post' action='index.php' > 
<input name='url' class='material' placeholder='Enter url...' type='url'autofocus/></br><br> 
<input name='submit' id='submit' type='submit' value='submit'/></br><br> 
</form> 
"; 

} 

您还可能有在顶部的开口<?php后添加ob_start();

而且文件没有用字节顺序标记编码。这也会引发错误。

该文件可以编码为ANSI或UTF-8 而不包含 BOM。

+0

错误:“无法打开流:连接超时”和“无法修改标题信息” –

+0

@SriCharan再次检查我的答案;我做了一些编辑。完全跟随他们。这是一个编码问题。 –

+0

@SriCharan欢迎。 –