在 PHP 中使用带有 cURL 的 try/catch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11297320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:12:25  来源:igfitidea点击:

Using a try/catch with cURL in PHP

phpcurl

提问by tadywankenobi

I thought I had resolved this but I obviously haven't and was hoping someone might be able to shed some light on what I am doing wrong. I am trying to get a piece of PHP code to check a file on a server and based on the response, perform an action. The file is a simple text file which has either the word "true" or "false" written on it. If the file exists or the return is "true" the script goes to one url. If it doesn't exist (i.e. server is unavailable) or the return is "false", the script goes to a second url. The following is the snippet of code I have used up until now.

我以为我已经解决了这个问题,但我显然还没有解决,希望有人能够解释我做错了什么。我正在尝试获取一段 PHP 代码来检查服务器上的文件,并根据响应执行操作。该文件是一个简单的文本文件,上面写有“真”或“假”字样。如果文件存在或返回为“true”,则脚本会转到一个 url。如果它不存在(即服务器不可用)或返回“false”,则脚本转到第二个 url。以下是我到目前为止使用的代码片段。

$options[CURLOPT_URL] = 'https://[domain]/test.htm';

$options[CURLOPT_PORT] = 443;
$options[CURLOPT_FRESH_CONNECT] = true;
$options[CURLOPT_FOLLOWLOCATION] = false;
$options[CURLOPT_FAILONERROR] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = 10;

// Preset $response var to false and output
$fb = "";
$response = "false";
echo '<p class="response1">'.$response.'</p>';

try {
    $curl = curl_init();
    echo curl_setopt_array($curl, $options);
    // If curl request returns a value, I set it to the var here. 
    // If the file isn't found (server offline), the try/catch fails and var should stay as false.
    $fb = curl_exec($curl);
    curl_close($curl);
}
catch(Exception $e){
    throw new Exception("Invalid URL",0,$e);
}

if($fb == "true" || $fb == "false") {
    echo '<p class="response2">'.$fb.'</p>';
    $response = $fb;
}

// If cURL was successful, $response should now be true, otherwise it will have stayed false.
echo '<p class="response3">'.$response.'</p>';

This example here, only goes through the handling of the file test. The content of "test.htm" is either "true" or "false".

这里的这个例子,只经历了文件测试的处理。“test.htm”的内容是“真”或“假”。

This may seem like a convoluted way of doing it, but the file is on a third party server (I have no control over this) and needs to be checked as the third party vendor may decide to force a failover to the second url, while doing maintenance work on the first url. Hence why I can't just check for the file's existance.

这似乎是一种令人费解的方法,但该文件位于第三方服务器上(我无法控制)并且需要检查,因为第三方供应商可能决定强制故障转移到第二个 url,而对第一个 url 进行维护工作。因此,为什么我不能只检查文件的存在。

I have tested on a local setup and it all behaves as expected. The problem arises when the file is not there. The $fb = curl_exec($curl); doesn't fail, it just returns an empty value. I had thought that, as this is on an IIS PHP server, the issue might be PHP related on the server, but I am now seeing this issue locally (on a Unix system).

我已经在本地设置上进行了测试,一切都按预期运行。当文件不存在时就会出现问题。$fb = curl_exec($curl); 不会失败,它只是返回一个空值。我原以为,由于这是在 IIS PHP 服务器上,问题可能与服务器上的 PHP 相关,但我现在在本地(在 Unix 系统上)看到了这个问题。

If anyone could shed any light on what I might be doing wrong, I'd really appreciate it. There may also be a much shorter way of testing this file, I could be going the long way around to do this, so really appreciate any help.

如果有人能阐明我可能做错了什么,我将不胜感激。可能还有一种更短的方法来测试这个文件,我可能会走很长的路来做到这一点,所以非常感谢任何帮助。

T

回答by Jonathan Cremin

cURL does not throw exceptions, it is a thin wrapper for the C library it binds to.

cURL 不会抛出异常,它是它绑定到的 C 库的瘦包装器。

I've modified your example to use a more correct pattern.

我已经修改了您的示例以使用更正确的模式。

$options[CURLOPT_URL] = 'https://[domain]/test.htm';

$options[CURLOPT_PORT] = 443;
$options[CURLOPT_FRESH_CONNECT] = true;
$options[CURLOPT_FOLLOWLOCATION] = false;
$options[CURLOPT_FAILONERROR] = true;
$options[CURLOPT_RETURNTRANSFER] = true; // curl_exec will not return true if you use this, it will instead return the request body
$options[CURLOPT_TIMEOUT] = 10;

// Preset $response var to false and output
$fb = "";
$response = false;// don't quote booleans
echo '<p class="response1">'.$response.'</p>';

$curl = curl_init();
curl_setopt_array($curl, $options);
// If curl request returns a value, I set it to the var here. 
// If the file isn't found (server offline), the try/catch fails and var should stay as false.
$fb = curl_exec($curl);
curl_close($curl);

if($fb !== false) {
    echo '<p class="response2">'.$fb.'</p>';
    $response = $fb;
}

// If cURL was successful, $response should now be true, otherwise it will have stayed false.
echo '<p class="response3">'.$response.'</p>';