cURL尝试捕获问题

时间:2011-08-15 19:38:50

标签: curl error-handling

我正在尝试调试此cURL - 操作。 var_dump()正在返回bool(false)

在这种情况下如何让它退出试用?

function parse($url, $headonly = TRUE ){
    $agents = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16';
    try
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $agents);
        curl_setopt($ch, CURLOPT_URL, $url);
        $curlResp = curl_exec($ch);
        curl_close($ch);
        var_dump($curlResp); //RETURNS bool(false)
        die;

        $resp = str_replace("class=l","class='l'",$curlResp);
        return $resp;
    }
    catch( Exception $e)
    {
        $strResponse = "";
        $strErrorCode = $e->getCode();
        $strErrorMessage = $e->getMessage();
        print_r($arrCurlInfo, $strErrorCode, $strErrorMessage);
        die;
    }  //end catch
}

1 个答案:

答案 0 :(得分:3)

为什么不在返回false时尝试抛出异常?

即。

if ($curlResp === FALSE) {
    throw new Exception(); 
}