在PHP中使用带有cURL的try / catch [英] Using a try/catch with cURL in PHP

查看:260
本文介绍了在PHP中使用带有cURL的try / catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我已经解决了这个问题,但我显然没有,并希望有人能够阐明我做错了什么。我试图获取一段PHP代码来检查服务器上的文件,并基于响应,执行操作。该文件是一个简单的文本文件,其上写有true或false。如果文件存在或返回值为true,脚本将转到一个URL。如果它不存在(即服务器不可用)或返回是false,脚本转到第二个url。以下是我目前使用的代码片段。

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.

$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>';

这里的示例只处理文件测试。 test.htm的内容是true或false。

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

这可能看起来像一个复杂的方式,但文件是在第三方服务器(我没有控制这一点),并需要检查,因为第三方供应商可能决定强制故障转移到第二个URL,同时在第一个URL进行维护工作。因此,为什么我不能只检查文件的存在。

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.

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

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).

如果任何人都能看出我可能做错了什么,我真的很感激。也许有一个更短的方式来测试这个文件,我可以走很长的路要做,所以真的很感激任何帮助。

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

推荐答案

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

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

我修改了你的例子以使用更正确的模式。

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>';

这篇关于在PHP中使用带有cURL的try / catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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