Jump to content

The cURL http_code Property


iwato

Recommended Posts

Background: Please find listed below a block of code taken from a larger block of code that forms a cURL information transfer.Question: Having already checked to see that $ret is non-empty, why is it necessary to determine whether or not $info['http_code'] is also non-empty? Is it simply performed, so as not to echo a needless statement, or is there something inherently important about value of the http_code property that must always be known before proceeding with a cURL run?

$ret = curl_exec($ch)if (empty($ret)) {	die(curl_error($ch));	curl_close($ch);} else {	$info = curl_getinfo($ch);	curl_close($ch);		if (empty($info['http_code'])) {			die("No HTTP code was returned");	} else {		echo 'The server responded: ' . $info['http_code']; echo '<br /><br />';	}	}

Roddy

Link to comment
Share on other sites

Without looking into the details of cURL (just general programming guidelines), since $ret comes from curl_exec and $info comes from curl_getinfo, it may not necessarily be the case that the http_code property is always present if $ret is non-empty. In terms of cURL that might be true, http_code might always be populated if $ret is non-empty, but from a general programming practice point of view that's not necessarily the case because they come from different functions.Even so, it may still be the case that the values that get returned from curl_getinfo might differ regardless of whether or not curl_exec returned an empty value or not. But the general guideline is that it's just good practice to check for things like that instead of making assumptions about the data. It might only be an edge case if http_code is not present even if the response succeeded, but it's good practice to have your code check for that instead of assuming it's there and possibly creating an error condition that you aren't handling. For example, a server following an old HTTP spec might behave differently than more recent servers.

Link to comment
Share on other sites

But the general guideline is that it's just good practice to check for things like that instead of making assumptions about the data.
Learning how to be a good programmer.Many thanks.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...