Verification: a143cc29221c9be0

Php add header to curl

Содержание

Why?

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 http://www.example.com/2 and sets the referring page as http://www.example.com/1. The latter URL will then show up in the weblogs as the page the visitor came from. The output from http://www.example.com/2 is then saved to the $html variable.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/1');
$html = curl_exec($ch);

CURLOPT_REFERER

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