【问题标题】:Using a try/catch with cURL in PHP在 PHP 中使用带有 cURL 的 try/catch
【发布时间】:2012-07-02 16:10:26
【问题描述】:

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

$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”的内容要么是“真”,要么是“假”。

这可能看起来很复杂,但文件位于第三方服务器上(我无法控制)并且需要检查,因为第三方供应商可能决定强制故障转移到第二个服务器url,同时对第一个 url 进行维护工作。因此,为什么我不能只检查文件是否存在。

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

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

T

【问题讨论】:

  • 除了你的try-catch块抛出的异常之外,你不能只在$fb为空时抛出一个异常吗?
  • 真的不需要在 $fb 为空时抛出异常。响应变量设置为 false,所有 $fb 用于检查状态是否不同,然后重置响应。但我越看它看起来越不对。我已经连续8个小时了,所以我再也看不到了!!!
  • 哦,我明白了。我非常注意您谈到 curl_exec() 失败并且您在 catch 语句中抛出了一个新异常并且没有足够仔细地阅读 cmets 的事实。

标签: php curl


【解决方案1】:

cURL 不会抛出异常,它是它所绑定的 C 库的精简包装器。

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

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

【讨论】:

  • 太棒了,干杯老兄,我会试一试的。一个 Q,您说不要引用布尔值,但服务器上文件的响应将是一个字符串。它会自动被视为布尔值吗?
  • 对此做了一点修改,也可以测试头部的状态码,而不是测试文件是否存在...
  • 我没有意识到您将它与字符串进行比较,但 $fb 要么是布尔值 false,要么是请求中返回的任何字符串。使用 PHP 的输入,这就是我使用强比较运算符 !== 的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 2016-10-26
  • 2013-07-07
  • 2015-02-27
相关资源
最近更新 更多