Some times I need to fetch a page with its headers. The problem is that the cURL library can return both but there is no default way to ask for both of them separate. So here is a work around which works for most of the cases.
Code:
<?php $ch = curl_init('http://twitter.com/'); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 1); // This will tell cURL to return the headers as well curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch);
// Here we will try to parse the headers : preg_match('/(.*)\x0d\x0a(.*)/s', $response, $matches); $respHeaders = explode( "\n", $matches[1]); $response = $matches[2];