将cURL设置为使用本地虚拟主机

使用Apache或Ngnix我总是创build基于真实项目,如http://project1.loc开发网站,添加到我的.hosts文件后,浏览器没有问题的使用。

但是,当我试图做一个cURL请求( http://project1.loc/post.json )到同一个URL,我从来没有得到任何东西,但超时。 我假设cURL不关心我的自定义主机,并直接进入名称服务器的信息。

我怎样才能解决这个问题?

更新我设置自定义标题“主机: http://project1.loc ”,现在我得到400错误 – 但他们是瞬时的,所以我假设cURL至less使用主机文件…

编辑:虽然这是目前接受的答案,读者可能会发现用户约翰·哈特更多的其他答案适应他们的需求。 根据用户Ken的说法,它使用了一个在7.21.3版本( 2010年12月发布 ,即在这个初始答案之后)中引入的选项。


在你编辑的问题中,你使用URL作为主机名,而它只需要是主机名。

尝试:

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

其中project1.loc 只是主机名, 127.0.0.1是目标IP地址。

(如果您使用的是库中的curl而不是命令行,请确保不要将http://放在Host标头中。)

实际上,curl有明确的选项:– --resolve

而不是curl -H 'Host: yada.com' http://127.0.0.1/something

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

你问有什么区别?

其中,这适用于HTTPS。 假设您的本地服务器拥有yada.com的证书,则上面的第一个示例将失败,因为yada.com证书与URL中的127.0.0.1主机名不匹配。

第二个示例与HTTPS正常工作。

本质上,通过-H传递一个“Host”头文件会将你的主机攻击到头文件集中,但绕过curl的所有主机特定的智能。 使用--resolve利用所有正常的逻辑适用,但只是假装DNS查找返回您的命令行选项中的数据。 它就像/etc/hosts应该一样工作。

注意--resolve需要一个端口号,所以你可以使用HTTPS

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

请使用指向127.0.0.1的真正完全合格的域名(如dev.yourdomain.com ),或者尝试编辑正确的主机文件(通常是* nix环境中的/ etc / hosts)。

看来这不是一个不常见的问题。

先检查一下 。

如果这样做没有帮助,则可以在Windows上安装本地DNS服务器,如此。 将Windowsconfiguration为使用本地主机作为DNS服务器。 可以将此服务器configuration为对所需的任何虚假域具有权威性,并将请求转发给真正的DNS服务器以处理所有其他请求。

我个人认为这是有点过分,并不能明白为什么hosts文件不起作用。 但它应该解决你遇到的问题。 确保你设置了正常的DNS服务器作为转发器。

服务器是否真的获得了请求,并且你是否正确地处理了主机名(别名)?

添加到我的.hosts文件后

检查您的networking服务器日志,看看请求是如何进来的…

curl有选项可以转储发送的请求,并且收到响应,这就叫trace,它会被保存到一个文件中。

– 跟踪

如果您缺less主机或标题信息 – 您可以使用config选项强制这些标题。

我会得到在命令行工作的curl请求,然后尝试在PHP中实现。

configuration选项是

-K / – configuration

curl相关的选项在这里

–trace启用对给定输出文件的所有传入和传出数据(包括描述性信息)的完整跟踪转储。 使用“ – ”作为文件名将输出发送到标准输出。

  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 / – config指定从哪个configuration文件读取curl参数。 configuration文件是一个文本文件,可以在其中写入命令行参数,然后将其用作实际命令行上的文本。 选项及其参数必须在同一configuration文件行中指定,用空格,冒号,等号或其任意组合(但是,优选分隔符为等号)分隔。 如果参数要包含空格,则参数必须用引号括起来。 在双引号内,可以使用下列转义序列:\,\“,\ t,\ n,\ r和\ v,任何其他字母前面的反斜杠都会被忽略,如果configuration行的第一列是'#'字符,其余的行将被视为注释,只能在configuration文件中每个物理行写一个选项。

  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. 

提出要求

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

导致在-H日志文件包含:

 == 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 

我的主机文件如下所示:

 # 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