2

Ineed to make a request exactly like the browser does it with CURL and PHP, when a copy as Curl (cmd) i see this headers:

curl "https://www.w3schools.com/js/ajax_info.txt" 
-H "Sec-Fetch-Mode: cors" 
-H "referer: https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_first" 
-H "Origin: https://www.w3schools.com" 
-H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"  
-H "dnt: 1" 
--compressed

I have some code but i need more options, for example the part "--compressed" i do not have idea how to include it at request

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'https://www.w3schools.com/js/ajax_info.txt');

$data = curl_exec($ch);
curl_close($ch);
return $data;

?>

i can nnot see a result beacause are headers

Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37

1 Answers1

1

You set the CURLOPT_ENCODING option and ask for specific encodings (compression methods) or let curl itself ask for the encodings it knows of. Unless you have special knowledge, letting curl ask for what it supports generally gets the best results.

curl_setopt($ch, CURLOPT_ENCODING, '');

Note that the neat curl-to-php service mentioned in a comment works for this but will generate the non-optimal version with a fixed set of methods. I've filed a separate bug report on that.

This option is called CURLOPT_ACCEPT_ENCODING in the underlying libcurl, as it was renamed there many years ago.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222