cant set Host in CURL PHP cant set Host in CURL PHP php php

cant set Host in CURL PHP


Assuming we are not trying spoof the Host header, omit the Host header altogether and let curl sort it out. In this case, just remove 'Host: hostname', because you already get curl to automatically set this with your code near the bottom with curl_setopt($ch, CURLOPT_URL, $url);.

If you really want to set the Host header yourself, then just replace

'Host: hostname',

with

"Host: ". parse_url($url, PHP_URL_HOST),

(Note: This function doesn't work with relative URLs.)


try like this,

  curl_init('XXX.XXX.XXX.XXX');  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));


If you are using windows and xampp then try to use virtual host rather than localhost, then it will start working, I did the same.