错误检查和处理是程序员的朋友。检查初始化和执行cURL函数的返回值。
curl_error()并
curl_errno()在失败的情况下包含更多信息:
try { $ch = curl_init(); // Check if initialization had gone wrong* if ($ch === false) { throw new Exception('failed to initialize'); } curl_setopt($ch, CURLOPT_URL, 'http://example.com/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(); $content = curl_exec($ch); // Check the return value of curl_exec(), too if ($content === false) { throw new Exception(curl_error($ch), curl_errno($ch)); } // Close curl handle curl_close($ch);} catch(Exception $e) { trigger_error(sprintf( 'Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);}*在
curl_init()手动状态:
成功返回cURL句柄,错误返回 FALSE 。
我观察到该函数
FALSE在您使用其
$url参数且无法解析域时会返回。如果未使用该参数,则该函数 可能
永远不会返回
FALSE。但是,请务必始终进行检查,因为该手册并未明确说明实际的“错误”。



