2016-11-23 15 views
0

PHPファイルを使用してURLから自分のコンピュータに画像を保存しようとしています。私が検索したカードが1つの名前しか持たないときに動作します。しかし、私がしようとすると、複数の名前を持つカードのためにそれを行うには、私が取得: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

ありがとうございました! –

関連する問題