Verification: a143cc29221c9be0

Php add header to curl



There may be times you need to do this if you need to scrape a web page which is expecting some sort of referring URL, or in order to test some code in your own project which requires a refering url to be sent. There are other blackhat reasons but I won’t go into those here (and nor do I use them myself).

The PHP code

The following PHP example requests the page at and sets the referring page as The latter URL will then show up in the weblogs as the page the visitor came from. The output from is then saved to the $html variable.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, '');
$html = curl_exec($ch);


The CURLOPT_REFERER line is where the referer string is set. Simply set whatever URL you require as the string passed to it.