How to use Curl with HEADERS?

I tried to do something similar, but it did not work!

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://google.com/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ch, CURLOPT_HTTPHEADER, array('GET /search?q=kk HTTP/1.1 Host: www.google.de User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-gb,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Proxy-Connection: Close Cookie: PREF=ID=2bb051bfbf00e95b:U=c0bb6046a0ce0334: Cache-Control: max-age=0 Connection: Close ')); $response = curl_exec($ch); curl_close($ch); echo $response; 

Also, is it possible to make an entire request with headers only without setting a URL? I mean without this?

 curl_setopt($ch, CURLOPT_URL, "http://google.com/"); 

Thanks!

+4
source share
5 answers

I got it for work.

1) Change the header of Host: www.google.de to Host: www.google.com

Motivation: the host specified in the Host header must exactly match the host URL.

2) Use "www.google.com" instead of "google.com"

Motivation: Searches made on google.com will not receive search results. You will be prompted to go to www.google.com.

3) Specify the full URL in CURLOPT_URL, and not just the host name. For instance. change the value of CURLOPT_URL to curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?q=kk");

Motivation: proper use of cURL API.

4) Remove GET /search?q=kk HTTP/1.1 from CURLOPT_HTTPHEADER - it is inappropriate.

Motivation: proper use of cURL API.

5) The response will be compressed by gzip or deflate. To stop this, remove the Accept-Encoding: gzip, deflate request header Accept-Encoding: gzip, deflate .

Motivation: If you tell Google that you can get a concise response, it will send you a message. Decompressing an HTTP response is an additional step that you might not want to take. It may be easier to handle the answer if it is in uncompressed text form.

+4
source

To add what other posters said, you also cannot insert the GET command into the CURLOPT_HTTPHEADER array, because this is specified in other cURL parameters. cURL is designed to work using the curl_setopt function; you cannot get around it by placing your HTTP message in the header section. For example, to make sure your command is an HTTP GET operation, you set CURLOPT_HTTPGET to TRUE (although by default cURL will send GET until you change it to something else).

To solve the question of why you cannot get the correct URL, this is because you need to specify the entire path in CURLOPT_URL , and not just the host. So you really have to write curl_setopt($ch, CURLOPT_URL, "http://google.de/search?q=kk HTTP/1.1"); to set the url.

Also, I have no idea why you put Connection: Close in its HTTP headers for a GET request. In this heading, you tell Google that you are closing the connection that you have; this is handled by curl_close($ch); so forget about this heading. In fact, half of the elements in your HTTP headers do not have a place. For example, why do you send a cookie to a request to receive search results? Make sure you know what each header does before posting it. Otherwise, you absolutely cannot say whether you are sending the correct headers or not.

+4
source

You have a few problems, but they should be easy to figure out. First, you set the host in a non-host header in the URL request, but since you are using HTTP1.0, this is not necessary.

Secondly, you need each line in HTTPHEADER as its own thing in the array, and you do not include the GET line.

 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-gb,en;q=0.5', 'Accept-Encoding: gzip, deflate', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Proxy-Connection: Close', 'Cookie: PREF=ID=2bb051bfbf00e95b:U=c0bb6046a0ce0334:', 'Cache-Control: max-age=0', 'Connection: Close' )); 

(You obviously stole this from Firefox and the old version, but we will give it a slide.) Finally, yes, you must tell CURLOPT_URL that the cURL API is designed that way.

+2
source

If you need such a high level of control over the received HTTP request, I would recommend using raw socket functions to manually send the request instead. The manual even has an example of making an HTTP request using fsockets:

 $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } 
+1
source

Next time you should carefully read the exact manual. An example of adding fields: array('Content-type: text/plain', 'Content-length: 100') , and not all in one line, but the fields are separate elements of the array.

+1
source

Source: https://habr.com/ru/post/1399457/


All Articles