Install cURL to use local virtual hosts

Using Apache or Ngnix. I always create development sites based on real projects, such as http://project1.loc , which, after adding to my .hosts file .hosts browser has no problems using it.

However, when I try to make a cURL request ( http://project1.loc/post.json ) to the same URL, I never get anything but a timeout. I assume cURL does not care about my own hosts and goes directly to the name server for information.

How can i fix this?

UPDATE I set the custom header "HOST: http: //project1.loc " and now I get 400 errors - but they are instantaneous, so I assume cURL at least uses the hosts file ...

+67
curl hosts
Aug 02 '10 at 18:14
source share
6 answers

EDIT. Although this is a currently accepted answer, readers may find this other answer by John Hart more suited to their needs. It uses the option which, according to Ken , was introduced in version 7.21.3 (which was released in December 2010 , i.e. after this initial response).




In your edited question, you use the URL as the host name, whereas it should only be the host name.

Try:

 curl -H 'Host: project1.loc' http://127.0.0.1/something 

where project1.loc is just the host name and 127.0.0.1 is the destination IP address.

(If you are using curl from the library, and not on the command line, make sure you do not put http:// in the Host header.)

+80
Aug 11 '10 at
source share

Actually, curl has an option explicitly for this: --resolve

Instead of curl -H 'Host: yada.com' http://127.0.0.1/something

use curl --resolve 'yada.com:80:127.0.0.1' http://yada.com/something

What is the difference, you ask?

In particular, it works with HTTPS. Assuming your local server has a certificate for yada.com , the first example above will fail because the yada.com certificate yada.com not match the host name 127.0.0.1 in the URL.

The second example works correctly with HTTPS.

Essentially, passing the β€œHost” header with -H does hack your host in the set of headers, but bypasses all host-specific characteristics. Using --resolve uses all the usual logic used, but it just pretends that a DNS lookup returns the data in the command line parameter. It works the same way as /etc/hosts should.

Note --resolve accepts a port number, so for HTTPS you should use

curl --resolve 'yada.com:443:127.0.0.1' https://yada.com/something

+270
Apr 18 2018-12-18T00:
source share

Use either the real fully qualified domain name (for example, dev.yourdomain.com ) pointing to 127.0.0.1 , or try editing the correct hosts file (usually / etc / hosts in * nix environments).

+1
Aug 2 '10 at 18:20
source share

This does not seem to be an unusual problem.

Check this out first.

If this does not help, you can install a local DNS server in Windows, for example this . Configure Windows to use localhost as the DNS server. This server can be configured to be authoritative for any fake domains you need and forward requests to real DNS servers for all other requests.

I personally think this is a little higher, and cannot understand why the hosts file is not working. But he must solve the problem that you are facing. Make sure you also configure your regular DNS servers as senders.

+1
Aug 10 '10 at 0:24
source share

Does the server really receive requests and are you handling the hostname (alias) correctly?

after adding .hosts to my file

Check your web server’s log to see how the request came ...

curl has options for sending a sent request and a received response, it is called trace, which will be saved in a file.

- trace

If you are missing host or header information, you can force these headers to be configured using the configuration option.

I would get a curl request running on the command line and then try to implement in PHP.

configuration parameter

-K / - config

the parameters that matter in curl are here

- trace Includes a complete dump of all incoming and outgoing data, including descriptive information, in this output file. Use "-" as the file name for output sent to standard output.

  This option overrides previous uses of -v/--verbose or --trace-ascii. If this option is used several times, the last one will be used. 

-K / - configurations Specify which configuration file to read using the curl arguments. A configuration file is a text file in which command line arguments can be written, which will then be used as if they were written on the actual command line. Parameters and their parameters must be indicated on the same line of the configuration file, separated by a space, a colon, an equal sign, or any combination of them (however, the preferred torus separator is an equal sign). If the parameter must contain spaces, the parameter must be enclosed in quotation marks. The following escape sequences are available in double quotes: \, \ ", \ t, \ n, \ r, and \ v. The backslash preceding any other letter is ignored. If the first column of the configuration line is the character '#', the rest part of the line will be considered as a comment, just write one option behind the physical line in the configuration file.

  Specify the filename to -K/--config as '-' to make curl read the file from stdin. Note that to be able to specify a URL in the config file, you need to specify it using the --url option, and not by simply writing the URL on its own line. So, it could look similar to this: url = "http://curl.haxx.se/docs/" Long option names can optionally be given in the config file without the initial double dashes. When curl is invoked, it always (unless -q is used) checks for a default config file and uses it if found. The default config file is checked for in the following places in this order: 1) curl tries to find the "home dir": It first checks for the CURL_HOME and then the HOME environment variables. Failing that, it uses getpwuid() on UNIX-like systems (which returns the home dir given the current user in your system). On Windows, it then checks for the APPDATA variable, or as a last resort the '%USERPROFILE%\Application Data'. 2) On windows, if there is no _curlrc file in the home dir, it checks for one in the same dir the curl executable is placed. On UNIX-like systems, it will simply try to load .curlrc from the deter- mined home dir. # --- Example file --- # this is a comment url = "curl.haxx.se" output = "curlhere.html" user-agent = "superagent/1.0" # and fetch another URL too url = "curl.haxx.se/docs/manpage.html" -O referer = "http://nowhereatall.com/" # --- End of example file --- This option can be used multiple times to load multiple config files. 
0
Aug 08 '10 at 19:46
source share

Query execution

 C:\wnmp\curl>curl.exe --trace-ascii -H 'project1.loc' -d "uuid=d99a49d846d5ae570 667a00825373a7b5ae8e8e2" http://project1.loc/Users/getSettings.xml 

The result in a -H log file containing:

 == Info: Could not resolve host: 'project1.loc'; Host not found == Info: Closing connection #0 == Info: About to connect() to project1.loc port 80 (#0) == Info: Trying 127.0.0.1... == Info: connected == Info: Connected to project1.loc (127.0.0.1) port 80 (#0) => Send header, 230 bytes (0xe6) 0000: POST /Users/getSettings.xml HTTP/1.1 0026: User-Agent: curl/7.19.5 (i586-pc-mingw32msvc) libcurl/7.19.5 Ope 0066: nSSL/1.0.0a zlib/1.2.3 007e: Host: project1.loc 0092: Accept: */* 009f: Content-Length: 45 00b3: Content-Type: application/x-www-form-urlencoded 00e4: => Send data, 45 bytes (0x2d) 0000: uuid=d99a49d846d5ae570667a00825373a7b5ae8e8e2 <= Recv header, 24 bytes (0x18) 0000: HTTP/1.1 403 Forbidden <= Recv header, 22 bytes (0x16) 0000: Server: nginx/0.7.66 <= Recv header, 37 bytes (0x25) 0000: Date: Wed, 11 Aug 2010 15:37:06 GMT <= Recv header, 25 bytes (0x19) 0000: Content-Type: text/html <= Recv header, 28 bytes (0x1c) 0000: Transfer-Encoding: chunked <= Recv header, 24 bytes (0x18) 0000: Connection: keep-alive <= Recv header, 25 bytes (0x19) 0000: X-Powered-By: PHP/5.3.2 <= Recv header, 56 bytes (0x38) 0000: Set-Cookie: SESSION=m9j6caghb223uubiddolec2005; path=/ <= Recv header, 57 bytes (0x39) 0000: P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM" <= Recv header, 2 bytes (0x2) 0000: <= Recv data, 118 bytes (0x76) 0000: 6b 0004: <html><head><title>HTTP/1.1 403 Forbidden</title></head><body><h 0044: 1>HTTP/1.1 403 Forbidden</h1></body></html> 0071: 0 0074: == Info: Connection #0 to host project1.loc left intact == Info: Closing connection #0 

My hosts file looks like this:

 # Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host 127.0.0.1 localhost ... ... 127.0.0.1 project1.loc 
0
Aug 11 '10 at 15:40
source share



All Articles