2016-11-23 127 views
0

我试图使用PHP文件将图像从URL保存到我的计算机。它适用于我搜索的卡片只有一个名称;然而,当我尝试做它用多个名字的卡片,我得到:使用PHP将图像从URL保存到磁盘

Warning: file_get_contents(http://gatherer.wizards.com/Handlers/Image.ashx?name=black lotus&type=card&.jpg): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\xampp\htdocs\MTGFetcher\results.php on line 6

这是我为我的cardfetch.php代码;

<form action="results.php" method="post"> 

Name of card: <br> 

<input type="text" name="cardname" value="cardresult"> 

<input type="submit" value="Submit"> 

</form> 

而对于我results.php:

<?php 

$results = $_POST['cardname']; 
$url = "http://gatherer.wizards.com/Handlers/Image.ashx?name=" . $results . "&type=card&.jpg"; 
$img = "Pictures/$results.jpg"; 
file_put_contents($img, file_get_contents($url)); 
?> 

<img src="<?php echo $url ?>"> 

为什么不与多个单词的工作?

回答

1

该空间可能会丢掉东西。使用http_build_query,它会转换任何坏字符:

$query_array = array('name'=>$_POST['cardname'], 'type'=> 'card', '.jpg'); 
$url = "http://gatherer.wizards.com/Handlers/Image.ashx?".http_build_query($query_array); 

您可能必须与阵列玩,因为"&type=card&.jpg";是有点奇怪。

+0

非常感谢,工作! –