How replace file_get_contents with Zend_Http_Client ?(differnce in encoding etc..)

3.1k views Asked by At

How replace file_get_contents with Zend_Http_Client ?(differnce in encoding etc..)

code that should be replaced:

$url='http://google.com';$timeout=60;
$t = stream_context_create(array('http' => array('timeout' => $timeout)));
$content = @file_get_contents($url,0,$t);

My Solution:

$url='http://google.com';$timeout=60;
$client = new Zend_Http_Client($url, array('timeout' => $timeout));
$content=$client->request()->getBody();

please do you have better solution, does my solution have weak parts?


Edit:Improved solution

function getResponse($url='http://google.com',$timeout=60){
    $client = new Zend_Http_Client($url, array('timeout' => $timeout));
    if($content->isError())    {
            return null;
    }
    return $content->getBody();
 }

Remark: event better can be use of curl adapter that doing work much faster.

Thanks, Yosef

1

There are 1 answers

4
Tomáš Fejfar On BEST ANSWER

It's ok. You can also check what the response code is and act based on that. You can get codes like 500, 404 or 403 in some cases.