6, November 2009

Webdigity tutorials : cURL: separating headers from content

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
cURL: separating headers from content

Subscribe to our tutorials feed!

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($chCURLOPT_VERBOSE1);
curl_setopt($chCURLOPT_NOBODY0);
curl_setopt($chCURLOPT_HEADER1); // This will tell cURL to return the headers as well
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$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];

$headers = array();
foreach( 
$respHeaders as $r ){
$tmp explode(': '$r);
if ( count($tmp) == )
$headers[$tmp[0]] = trim($tmp[1]);
}
curl_close($ch);
?>


You can then execute the above to see the result:

Code:
<?php
echo '<pre>';
var_dump$headers );
var_dump$response);
?>

Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 06, 2009, 09:50:52 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2009 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.