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

将cURL设置为使用本地虚拟主机,curl,hosts,Curl,Hosts,使用Apache或Ngnix,我总是根据实际项目创建开发站点,例如http://project1.loc在添加到我的.hosts文件后,浏览器使用它没有问题 但是,当我尝试发出cURL请求时(http://project1.loc/post.json)到同一个URL,除了超时之外,我什么也得不到。我假设cURL不关心我的自定义主机,直接到名称服务器获取信息 我怎样才能解决这个问题 更新 我设置了一个自定义标题“HOST:”,现在我收到了400个错误-但是它们是即时的,所以我假设cURL至少在使用

使用Apache或Ngnix,我总是根据实际项目创建开发站点,例如
http://project1.loc
在添加到我的
.hosts
文件后,浏览器使用它没有问题

但是,当我尝试发出cURL请求时(
http://project1.loc/post.json
)到同一个URL,除了超时之外,我什么也得不到。我假设cURL不关心我的自定义主机,直接到名称服务器获取信息

我怎样才能解决这个问题

更新
我设置了一个自定义标题“HOST:”,现在我收到了400个错误-但是它们是即时的,所以我假设cURL至少在使用hosts文件…

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

服务器是否实际收到请求,您是否正确处理主机名(别名)

添加到my.hosts文件后

检查您的Web服务器日志,查看请求是如何传入的

curl具有转储发送的请求和接收的响应的选项,称为trace,它将保存到一个文件中

--痕迹

如果缺少主机或标头信息,可以使用配置选项强制这些标头

我会让curl请求在命令行上运行,然后尝试在PHP中实现

配置选项是

-K/--config

与curl相关的选项如下所示

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

      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 指定要从中读取curl参数的配置文件。该配置文件是一个文本文件,可以在其中写入命令行参数,然后将使用该文件,就好像它们是在实际数据库中写入的一样 选项及其参数必须在同一配置文件行中指定,用空格、冒号、等号或它们的任意组合分隔(但是,首选的separa- tor是等号)。如果参数要包含空格,则参数必须用引号括起来。在双引号中,以下转义序列可用:\,\“,\t\n, \r和\v.忽略任何其他字母前面的反斜杠。如果配置行的第一列是“#”字符,则该行的其余部分将被视为注释。每个字符只写一个选项 配置文件中的物理行

      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.

这似乎不是一个罕见的问题

先检查一下

如果没有帮助,您可以在Windows上安装本地DNS服务器,例如。将Windows配置为使用localhost作为DNS服务器。可以将此服务器配置为对您需要的任何伪域具有权威性,并将所有其他请求的请求转发到真实DNS服务器

我个人认为这有点过头了,不明白为什么hosts文件不能工作。但它应该可以解决您的问题。确保您也将您的普通DNS服务器设置为转发器。

编辑:虽然这是目前公认的答案,但读者可能会发现用户更适合他们的需要。它使用了一个选项,据用户称,在版本7.21.3中引入了,也就是说,在这个初始答案之后


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

尝试:

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

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

请求

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

实际上,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

本质上,传递一个“主机”“header via
-H
确实会将您的主机侵入header集合,但会绕过curl的所有主机特定智能。使用
--resolve
利用所有适用的正常逻辑,但只是假装DNS查找在您的命令行选项中返回了数据。它的工作原理与
/etc/hosts
应该的一样。”

注意
--resolve
需要一个端口号,因此对于HTTPS,您可以使用


curl——解析'yada.com:443:127.0.0.1'https://yada.com/something

要在尚未通过DNS连接的Apache http服务器上设置虚拟主机,我喜欢使用:

curl -s --connect-to ::host-name: http://project1.loc/post.json
其中主机名是运行web服务器的计算机的IP地址或DNS名称。
这也适用于https站点。

我在windows上使用
system32/drivers/etc/hosts进行开发
您使用的是cURL的本机版本还是cygwin交叉版本?我这样说是因为我不确定每个人如何解析其DNS。本机应该从windows的主机文件中获取,但cygwin版本可能需要cygwin版本。不管怎样,我们不管怎么设置,创建一个指向127.0.0.1的真实域都可以。我使用PHP5.3 for windows附带的本机windows构建(以PHP_fastcgi运行)。同样,我在windows上使用PHP在运行nginx的同一个windows上获取vhost上的页面。无论如何,我做了一个
# 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
curl -s --connect-to ::host-name: http://project1.loc/post.json