Sample code to grab the content of a remote page using curl

  • 2020-07-21 07:15:30
  • OfStack

The basic operation is as follows

$curlPost = 'a=1&b=2';// simulation POST data 
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:0.0.0.0', 'CLIENT-IP:0.0.0.0'));  // structure IP
curl_setopt($ch, CURLOPT_REFERER, "https://www.ofstack.com/");   // Tectonic background  
curl_setopt($ch,CURLOPT_URL, 'https://www.ofstack.com');// The path to the page that needs to be fetched 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);//post value 

$file_contents = curl_exec($ch);// The captured content is placed in a variable 
curl_close($ch)

Related articles: