2012-04-20 8 views
1

に失敗したHTTPリクエストをキャッチ、私はthis codeを使用しています...だからPHP

をのfile_get_contentsとPHPでのトラブルの少しを持っています。私はそれを見つけることができなかったことをハッシュ(bdfccf20b1db88d835c27685ac39f874)とそれを実行した場合

する前に、それは、これを返します:

fcf1eed8596699624167416a1e7e122e - found: octopus (Google) 
bed128365216c019988915ed3add75fb - found: passw0rd (Google) 
d0763edaa9d9bd2a9516280e9044d885 - found: monkey (Google) 
dfd8c10c1b9b58c8bf102225ae3be9eb - found: 12081977 (Google) 
ede6b50e7b5826fe48fc1f0fe772c48f - found: 1q2w3e4r5t6y (Google) 
bdfccf20b1db88d835c27685ac39f874 
Warning: file_get_contents(http://md5.gromweb.com/query/bdfccf20b1db88d835c27685ac39f874): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found 

in /Users/mihir/MD5Decryptor.php on line 44 

Catchable fatal error: Argument 2 passed to MD5Decryptor::dictionaryAttack() must be an array, boolean given, called in /Users/mihir/MD5Decryptor.php on line 56 and defined in /Users/mihir/MD5Decryptor.php on line 25 

が警告を停止するには、私はライン上

if ($response = file_get_contents($url)) { 

を変更しました

$response = @file_get_contents($url); 
if ($response) { 

と出力

なるまで
fcf1eed8596699624167416a1e7e122e - found: octopus (Google) 
bed128365216c019988915ed3add75fb - found: passw0rd (Google) 
d0763edaa9d9bd2a9516280e9044d885 - found: monkey (Google) 
dfd8c10c1b9b58c8bf102225ae3be9eb - found: 12081977 (Google) 
ede6b50e7b5826fe48fc1f0fe772c48f - found: 1q2w3e4r5t6y (Google) 
bdfccf20b1db88d835c27685ac39f874 
Catchable fatal error: Argument 2 passed to MD5Decryptor::dictionaryAttack() must be an array, boolean given, called in /Users/mihir/MD5Decryptor.php on line 56 and defined in /Users/mihir/MD5Decryptor.php on line 25 

どのようにエラーをキャッチできますか?で、ハッシュが見つからない場合は、どのように私は "ハッシュが見つかりません"と完全にクラッシュしないようにスクリプトを変更することができますか?事前に

おかげで...

答えて

4

あなたはまだエラーを取得している理由は、この行は次のとおりです。

return $this->dictionaryAttack($hash, $this->getWordlist($hash)); 

getWordListがfile_get_contents()から404を取得し、FALSEが返され、それが無効な引数が渡さ取得に関する例外を発生させています。あなたはそれを修正するために何しようとすることができ

一つはこれです:少なくともURLをそれ傾ける負荷を処理する必要があり

$list = $this->getWordlist($hash); 
if ($list === false) { 
    return 'Error fetching URL'; 
} else { 
    return $this->dictionaryAttack($hash, $list); 
} 

+0

固定、ありがとう:) – citruspi

+0

"3分で回答を受け入れることができます" ...あなたに会いましょう。 – citruspi

+2

代わりに、42行目の '$ list = FALSE'は' $ list = array() 'に置き換えることができます。 – Jasper

0

あなたができる最善のことはswitch to using cURLです。あなたはcan get the errors when using file_get_contents()ですが、あまり強くありません。

+0

うーん...私はcURLで多くの経験がありません。どのように、どこで、私はそれを実装しますか? – citruspi

+0

@MihirSingh、始めに役立つリンクを追加しました。 http://davidwalsh.name/download-urls-content-php-curl参照:http://php.net/manual/en/book.curl.php – Brad

+0

file_get_contentsでエラー処理を制御することもできます。 HTTPコンテキスト優れた作品、カールIMHOより悪くはない。 – hakre

1

try-catchブロックにすべてをラップします。 PHPにはこのような致命的なエラーを処理する仕組みがあります。

このような何かが動作するはずです:

try { 
    if ($response = file_get_contents($url)) { 
     ... 
    } 
} 
catch (Exception $e) { 
    // return your "Hash Not Found" response 
} 

ここでは構造上のいくつかのドキュメントです:あなたはおそらく、エラーを引き起こしている正確にどのコードの行を決定したいと思う http://php.net/manual/en/language.exceptions.php

、および使用あなたができる例外の最も特定のサブクラスです。これは、この問題とは無関係の例外を逃したくないため、ベストプラクティスです。