Search code examples
phpzend-frameworkfile-get-contentszend-http-client

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


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


Solution

  • 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.