2

How does I force curl to include the complete URL in the HTTP GET request?

Curl sends (not working):

GET /some/path HTTP/1.1
Host: my-domain-here.com
...

I want it to be (working):

GET http://my-domain-here.com/some/path HTTP/1.1
Host: i2.wp.com

So I want the host to be always included in the GET line. How can I do this using CURL/PHP? The server can only handle absolute URLs.

oberlies
  • 11,503
  • 4
  • 63
  • 110
Fredrik
  • 893
  • 2
  • 10
  • 20

4 Answers4

2

The PHP cURL wrapper does not expose a way of doing this as far as I know.

Also, cURL will automatically change the Host header even if you specify a different one. For example:

curl -v --dump-header - -0 -H 'Host: my-domain.com' http://subdomain.my-domain.com/something.html

will ignore the custom header and send this:

GET /something.html HTTP/1.0
User-Agent: curl/7.35.0
Host: subdomain.my-domain.com
Accept: */*

What you can do is build the request manually:

$host = 'my-domain.com';
$path = 'http://subdomain.my-domain.com/something.html';

$fp = fsockopen($host, 80);

fputs($fp, "GET $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: 0\r\n");
fputs($fp, "Connection: close\r\n\r\n");

$result = ''; 
while(!feof($fp)) {
    $result .= fgets($fp, 128);
}

fclose($fp);

echo $result;
Community
  • 1
  • 1
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
2

At least from the command line, curl does send absolute URIs if you configure it to use a HTTP proxy for the request. Even if you're not using a proxy, you can specify that that it use the actual server as the proxy server, and your server will then receive the absolute URI in the request.

John Dough
  • 400
  • 3
  • 9
1

curl always acts as a correct HTTP client. The standard requires that the request target (i.e. what follows the GET) only consists of an absolute path and optionally a query.

So it is not possible to make curl send an absolute URL as request target to an origin server.

Community
  • 1
  • 1
oberlies
  • 11,503
  • 4
  • 63
  • 110
1

curl has --request-target option

The following command will

  • Connect to 127.0.0.1
  • Send request path as GET http://request.host.name/path/ HTTP/1.1
  • Set Host header to Host: host.header
curl http://127.0.0.1 -v \
  --request-target http://request.host.name/path/ \
  --path-as-is \
  -H "Host: host.header"
*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET http://request.host.name/path/ HTTP/1.1
> Host: host.header
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Server: nginx/1.14.0
< Date: Fri, 19 Aug 2022 10:14:14 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
<
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
Steely Wing
  • 16,239
  • 8
  • 58
  • 54