How does Zend_Http_Client handle failurs?

Jaxel

Well-known member
I have the following function in my mod:
Code:
	public function retrieveData($data)
	{
		$feed = "http://www.stopforumspam.com/api?".
			($data['username'] ? 'username='.$data['username'].'&' : '').
			($data['email'] ? 'email='.$data['email'].'&' : '').
			($data['ip'] ? 'ip='.$data['ip'].'&' : '').
			"f=json";

		$client = new Zend_Http_Client($feed);
		$feed = $client->request()->getBody();
		$json = json_decode($feed, true);

		return $json;
	}
Simple right? It connects to a JSON file, and parses it based on submitted data.

However, you can occasionally get errors such as:
Zend_Http_Client_Adapter_Exception: Unable to Connect to tcp://www.stopforumspam.com:80. Error #110: Connection timed out - library/Zend/Http/Client/Adapter/Socket.php:235
Generated By: Unknown Account, Yesterday at 9:26 PM

#0 /library/Zend/Http/Client.php(973): Zend_Http_Client_Adapter_Socket->connect('www.stopforumsp...', 80, false)
#1 /library/EWRutiles/Model/ForumSpam.php(17): Zend_Http_Client->request()
#2 /library/EWRutiles/ControllerPublic/Register.php(16): EWRutiles_Model_ForumSpam->checkDatabase(Array)
#3 /library/XenForo/FrontController.php(310): EWRutiles_ControllerPublic_Register->actionRegister()
#4 /library/XenForo/FrontController.php(132): XenForo_FrontController->dispatch(Object(XenForo_RouteMatch))
#5 /index.php(15): XenForo_FrontController->run()
#6 {main}
I would rather the system handled these failures silently, and the function returned "false". Is this possible?
 
Look at the exception thrown: it's a Zend_* exception. So you can simply wrap the code which throws this exception in a try... catch block (in your case, the request() method) and handle the failures elegantly. For example:

PHP:
$feed = '[...]';
$client = new Zend_Http_Client($feed);

try
{
	$response = $client->request();
}
catch (Zend_Http_Client_Adapter_Exception $e)
{
	// Handle failures
	return array();
}

$json = json_decode($response->getBody(), true);
return $json;
 
Like this?
Code:
	public function retrieveData($data)
	{
		$feed = "http://www.stopforumspam.com/api?".
			($data['username'] ? 'username='.rawurlencode($data['username']).'&' : '').
			($data['email'] ? 'email='.rawurlencode($data['email']).'&' : '').
			($data['ip'] ? 'ip='.rawurlencode($data['ip']).'&' : '').
			"f=json";

		$client = new Zend_Http_Client($feed);

		try
		{
			$feed = $client->request()->getBody();
		}
		catch (Zend_Http_Client_Adapter_Exception $e)
		{
			return false;
		}

		$json = json_decode($feed, true);

		return $json;
	}
 
Was looking at the docs for Zend_Http_Client...
You can simplify the code a lil' bit; getting rid of the manual URI building & encoding part.

PHP:
public function retrieveData(array $query)
{
	$query['f'] = 'json';

	$client = new Zend_Http_Client('http://www.stopforumspam.com/api');
	$client->setParameterGet($query);

	try
	{
		$feed = $client->request()->getBody();
	}
	catch (Zend_Http_Client_Adapter_Exception $e)
	{
		return false;
	}

	return json_decode($feed, true);
}
 
Top Bottom