1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How does Zend_Http_Client handle failurs?

Discussion in 'XenForo Development Discussions' started by Jaxel, Jan 4, 2011.

  1. Jaxel

    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:
    I would rather the system handled these failures silently, and the function returned "false". Is this possible?
     
  2. Shadab

    Shadab Well-Known Member

    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;
     
  3. Jaxel

    Jaxel Well-Known Member

    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;
    	}
     
  4. Shadab

    Shadab Well-Known Member

    That should work, yes.
     
  5. Shadab

    Shadab Well-Known Member

    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($feedtrue);
    }
     

Share This Page