Can't Set "Host:" Header with CURL Request

17,207

Solution 1

The Host header doesn't accept a full URL, but only a hostname.

In this case the solution is to replace this header:

"Host: $url"

with:

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

Solution 2

The Host header takes a hostname, not a URL. i.e. localhost, not http://localhost/work/myproject.

cURL should be generating it automatically from CURLOPT_URL though.

Solution 3

The Host header should not contain the protocol, only the host's name. So in this case all you need is localhost:

'Host: localhost'
Share:
17,207

Related videos on Youtube

Dzhuneyt
Author by

Dzhuneyt

A decade of industry experience, gaining some knowledge along the way in a wide array of technologies. Since 2012, I've been actively involved with Docebo SpA, one of the largest, award-winning, cloud-based eLearning platforms (source: http://www.brandonhall.com/excellenceawards/excellence-learning.php?year=2018) Currently serving as SCRUM master and Team Leader of a team of 5 developers @ Docebo. In parallel, I am the CTO ScavaSoft. A Bulgaria-based software development firm, that is in close partnership with Docebo. Inside ScavaSoft, I am responsible for supporting a team of ~20 developers in their career journey as web developers and also making sure things run smoothly in terms of local server architecture (Redis/MySQL databases, local development machines, etc). As a hobby, running a mobile app development studio @ http://HasMobi.com Occasionally I blog about programming @ http://blog.dzhuneyt.com Some of my open source projects @ https://github.com/Dzhuneyt Occasionally I blog about my other hobby - photography @ http://snimam.be. If you want some inspiration, you can check out my 500px gallery @ https://500px.com/dzhuneyt

Updated on September 18, 2022

Comments

  • Dzhuneyt
    Dzhuneyt over 1 year

    I am trying to make a CURL request with some basic authentication (through sending an encrypted header). However, when I try to set the "Host: _" header, the remote server responds with:

    Bad Request: Your browser sent a request that this server could not understand.

    Here's the code that makes the CURL call. Note that it works as soon as I comment out the "Host: url" header in the $http_header variable. However, it is used on the target server as part of the authentication procedure, so I can not simply remove it.

        $curl = curl_init();
    
        $opt = array(
            CURLOPT_URL=>$url,
            CURLOPT_RETURNTRANSFER=>1,
            CURLOPT_HTTPHEADER=>$http_header,
            CURLOPT_POST=>1,
            CURLOPT_POSTFIELDS=>$data_params,
            CURLOPT_CONNECTTIMEOUT=>5, // Timeout to 5 seconds
        );
        curl_setopt_array($curl, $opt);
    
        // $output contains the output string
        $output = curl_exec($curl);
    
        // it closes the session
        curl_close($curl);
    

    The contents of $http_header (an associative array):

    array
      0 => string 'Host: http://localhost/work/myproject'
      1 => string 'Content-Type: multipart/form-data'
      2 => string 'X-Authorization: Basicauth Z2xZjA2YzJhMzIxYTI1ZmIzZTgxYQ=='