在PHP中捕获失败的HTTP请求 [英] Catching Failed HTTP Request in PHP

查看:112
本文介绍了在PHP中捕获失败的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在使用file_get_contents的PHP时遇到了一些麻烦...

So, I'm having a little bit of trouble in PHP with a file_get_contents...

我正在使用此代码.

之前,如果我使用无法找到的哈希值运行它(bdfccf20b1db88d835c27685ac39f874),它将返回以下内容:

Before, if I ran it with a hash that it could not find (bdfccf20b1db88d835c27685ac39f874), it would return this:

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

如何捕获错误?像这样,如果找不到哈希,我该如何修改脚本以返回找不到哈希"并且不会完全崩溃?

How can I catch the error? As in, if the hash is not found, how could I modify the script to return "Hash Not Found" and not completely crash?

预先感谢...

推荐答案

仍然出现此错误的原因是由于以下原因:

The reason you are still getting the error is because of this line:

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

当getWordList从file_get_contents()获取404时,将返回FALSE并生成有关传递无效参数的异常.

When getWordList gets a 404 from file_get_contents(), FALSE is returned and that is generating the exception about the invalid argument getting passed.

您可以尝试解决的一件事是:

One thing you could try to do to fix it is this:

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

这至少应该处理它无法加载的网址.

That should at least handle URLs it cant load.

这篇关于在PHP中捕获失败的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆