B
B
Bogdan Pasechnik2014-03-26 18:29:00
Zend Framework
Bogdan Pasechnik, 2014-03-26 18:29:00

How to implement stable work through a proxy through Zend_Http_Client?

I'm trying to set up requests to the server through a proxy. I am using the Zend_Http_Client library.
Here is a simple query example

$config = array(
            'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
            'proxy_host' => '***.***.***.***',
            'proxy_port' => 3180,
        );
        $client = new Zend_Http_Client('http://google.com', $config);
        $response=$client->request();
        echo $response->getBody();

The proxy server drops the connection in about 30% of cases. Throws Zend_Http_Client_Adapter_Exception "Unable to Connect to ...host..."
I solved this problem this way so far
class ...
     public function ...
        $config = array(
            'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
            'proxy_host' => '***.***.***.***',
            'proxy_port' => 3180,
        );
        $client = new Zend_Http_Client('http://google.com', $config);
        $response=$this->tryConnect($client);
        echo $response->getBody();
    ...


    public function tryConnect(Zend_Http_Client $client, $i=1) {
        try {
            return $client->request();
        } catch(Zend_Http_Client_Adapter_Exception $exception) {
            if($i>7) {
                throw new CException($exception->getMessage());
            } else {
                return $this->tryConnect($client, ++$i);
            }
        }
    }
...

Roughly speaking, I make 7 attempts to fulfill the request. This is basically what I need. But I don't like how I reached this solution. I think there should be something more elegant. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2014-03-26
@AMar4enko

Take a proxy aggregator, like freeproxy.ru, cling to their API to get a list of proxies, get a list of proxies, take the first one from the list, try to download content through it. If it fails, remove the proxy from the list, try the next one.
Once every N minutes, update the proxy list.
Anonymous proxies (I see that you are trying to google through anonymous proxies) are such a thing that no one guarantees their performance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question