Jump to content

Validate response code from URL query

Featured Replies

Posted
// Create a URL object
$url = \IPS\Http\Url::external( $API_URL );

// Now fetch it
try
{
	$response = $url->request()->get();
} catch( \IPS\Http\Request\Exception $e ) {
	return FALSE;
} catch( \RuntimeException $e ) {
	return FALSE;
}

if ($response->httpResponseCode == 200) {
	return TRUE;
} else {
	return FALSE:
}

Is this the correct way to validate that a url request is successful?

Yes, assuming that you consider a response code other than 200 to be unsuccessful (many APIs, for instance, may return response codes other than 200 validly).

Archived

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

Recently Browsing 0

  • No registered users viewing this page.