11.6 Excluding or Including the Header from a cURL Transfer


11.6 Excluding or Including the Header from a cURL Transfer

You want to exclude or include the header from a cURL transfer.

Technique

If you are using the Net_Curl class from PEAR, the header is automatically not included. To change this, set the header property to true :

 <?php // Initialize the transfer $conn = new Net_Curl('http://www.php.net/index.php'); if (Net_Curl::isError($conn)) {     die(sprintf('Error [%d]: %s',                 $conn->getCode(), $conn->getMessage())); } // Include the header $conn->header = true; // Execute the transfer $res = $conn->execute(); if (Net_Curl::isError($res)) {     die(sprintf('Error [%d]: %s',                 $res->getCode(), $res->getMessage())); } print "The output of your transfer was:\n<br>\n"; print $res; ?> 

If you want to exclude the header from a transfer and you aren't using the Net_Curl class from PEAR, set the CURLOPT_HEADER option to :

 <?php // Initialize the Curl transfer $ch = curl_init('http://www.ispi.net/index.html'); if (!$ch) {     die(sprintf('Error [%d]: %s',                 curl_error($ch), curl_errno($ch))); } // Set transfer options curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Execute transfer $res = curl_exec($ch); if (!$res) {     die(sprintf('Error [%d]: %s',                 curl_errno($ch), curl_error($ch))); } // Free resources curl_close($ch); print "The results of your transfer were:\n<br>\n"; print $res; ?> 

Comments

The Net_Curl class from PEAR automatically sets the CURLOPT_HEADER option to , so if you want to include the header in the results of your transfer, you must set the $header property to a nonfalse value. If you are using the PHP extension interface, the header is included by default, so you are required to set the CURLOPT_HEADER option to a nontrue value manually to exclude the header in the results.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net