2

だから、私は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)) {

43行目から

$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

3 に答える 3

4

それでもエラーが発生する理由は、次の行が原因です。

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

getWordListがから404を取得するとfile_get_contents()FALSEが返され、無効な引数が渡されるという例外が生成されます。

それを修正するためにあなたがしようとすることができる1つのことはこれです:

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

少なくとも、ロードできないURLを処理する必要があります。

于 2012-04-20T22:53:21.840 に答える
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

エラーの原因となっているコード行を正確に特定し、可能な限り最も具体的なExceptionのサブクラスを使用することをお勧めします。この問題に関係のない例外を見逃したくないので、これはベストプラクティスです。

于 2012-04-20T22:51:40.977 に答える
0

できる最善のことは、cURLの使用に切り替えることです。を使用するとエラーが発生する可能性がありfile_get_contents()ますが、それほど堅牢ではありません。

于 2012-04-20T22:46:46.147 に答える