Search code examples
phpzend-frameworkcurlzend-http-client

Get HTTP Response code using Zend and Curl


I have a URL that can be accessible via HTTP or HTTPS. I want to send a HEAD or GET request, which ever is fastest and get the response code so I know whether the URL is up or down.

How do I do that using Zend_HTTP_Client? I used get_headers() function, but it's very very slow on some remote servers. I am not sure if it handles HTTPS.


Solution

  • You may not want to use Zend_Http_Client for this - use native PHP functions instead (like fsockopen since it seems you want this to be efficient).

    That said, this may work for you (and since it defaults to the socket adapter, it may not be that less efficient than using the native functions):

    $client = new Zend_Http_Client();
    $response = $client->setUri($uri)->request(Zend_Http_Client::HEAD);
    

    If not, you could try setting the cURL options manually.

    $adapter = new Zend_Http_Client_Adapter_Curl();
    $adapter->setCurlOption(CURLOPT_NOBODY, true);
    
    $client = new Zend_Http_Client();
    $client->setAdapter($adapter);
    $response = $client->setUri($uri)->request(Zend_Http_Client::HEAD);
    

    The code's not tested. Use at your own risk.