I suspect that this section of code is causing one of those fatal errors that terminates your script without so much as a goodbye. Kinda like trying to call a method of a non-object or a nonexistent method of some object.
It also occurs to me that I don't know whether to call curl_info or check curl_errno first? Or under what circumstances curl_exec returns FALSE? Also, is there any way I can check to see if $ch is an open curl resource before calling curl_close to avoid an E_WARNING being thrown? Does checking is_resource($ch) mean that I can be sure to avoid an E_WARNING when I call curl_close?
Any comments on how to improve this code would be most welcome:
// fetch the remote image
$ch = curl_init();
if ($ch === FALSE){
throw New Exception("curl_init failed");
}
MTLog::getInstance()->debug("curl_init complete in slot #" . $this->slot);
$res_incoming_image_file_handle = fopen($str_incoming_image_file_path, "w+");
if ($res_incoming_image_file_handle === FALSE){
throw New Exception("fopen failed for incoming image file");
}
MTLog::getInstance()->debug("incoming file handle (" . $str_incoming_image_file_path . ") opened, preparing to set curl options in slot #" . $this->slot);
curl_setopt($ch, CURLOPT_FILE, $res_incoming_image_file_handle);
curl_setopt($ch, CURLOPT_URL, $this->cleanUrl($this->pending_img['remote_url']));
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, 16);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// didn't work unless i found good cert bundle
// curl_setopt($ch, CURLOPT_CAINFO, EREP_CA_CERT_BUNDLE_FILE);
// try this instead
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
MTLog::getInstance()->debug("curl options set, preparing to curl-exec in slot #" . $this->slot);
$curlResult = curl_exec($ch);
if(!curl_errno($ch)){
$arr_curl_info = curl_getinfo($ch);
} else {
$curl_error_string = "CURL Error #" . curl_errno($ch) . ": " . curl_error($ch);
}
curl_close($ch);
unset($ch);
fclose($res_incoming_image_file_handle);
unset($res_incoming_image_file_handle);
// check for a variety of failure reasons:
$b_image_success = NULL;
$str_image_failure_reason = '';
// curl failure:
if($curlResult === FALSE){
$b_image_success = FALSE;
$str_image_failure_reason = 'curl_exec() returned false. ' . $curl_error_string;
} else {
$curlCheck = $this->checkCurlInfo($arr_curl_info, $this->pending_img['remote_url']);
unset($arr_curl_info);
if ($curlCheck['result']){
if (!is_readable($str_incoming_image_file_path) || (filesize($str_incoming_image_file_path) == 0)){
$b_image_success = FALSE;
$str_image_failure_reason = 'CURL fetched unreadable or empty file.';
} else {
// SUCCESS!
$b_image_success = TRUE;
}
} else {
$b_image_success = FALSE;
$str_image_failure_reason = $curlCheck['reason'];
}
unset($curlCheck);
}