129

Apache または Ngnix を使用して、私は常に実際のプロジェクトに基づいて開発サイトを作成しています。たとえばhttp://project1.loc、ファイルに追加した後.hosts、ブラウザは問題なく使用できます。

ただし、同じ URL に対して cURL リクエスト ( ) を実行しようとするとhttp://project1.loc/post.json、タイムアウト以外は何も得られません。cURL はカスタム ホストを気にせず、その情報を得るためにネーム サーバーに直接アクセスすると想定しています。

どうすればこれを修正できますか?

更新 カスタム ヘッダー「HOST: http://project1.loc」を設定すると、400 エラーが発生するようになりましたが、エラーは瞬時に発生するため、cURL は少なくともホスト ファイルを使用していると想定しています...

4

7 に答える 7

478

実際、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 で機能します。ローカル サーバーに の証明書があると仮定すると、証明書がURLのホスト名と一致しないyada.comため、上記の最初の例は失敗します。yada.com127.0.0.1

2 番目の例は、HTTPS で正しく動作します。

本質的に、経由で「Host」ヘッダーを渡すと、-Hホストがヘッダー セットにハックされますが、curl のホスト固有のインテリジェンスはすべてバイパスされます。を使用--resolveすると、適用されるすべての通常のロジックが活用されますが、DNS ルックアップがコマンドライン オプションでデータを返したふりをするだけです。本来のように機能し/etc/hostsます。

Note--resolveにはポート番号が必要なので、HTTPS の場合は使用します

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

于 2012-04-18T21:43:21.400 に答える
121

編集:これは現在受け入れられている回答ですが、読者は、ユーザーJohn Hartによるこの他の回答がニーズにより適していることに気付くかもしれません。ユーザー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ヘッダーを追加しないでください。)

于 2010-08-11T12:51:41.523 に答える
2

これは珍しい問題ではないようです。

まずこれを確認してください。

それでも問題が解決しない場合は、このなどのローカル DNS サーバーを Windows にインストールできます。localhost を DNS サーバーとして使用するように Windows を構成します。このサーバーは、必要な偽のドメインに対して権限を持ち、他のすべての要求については実際の DNS サーバーに要求を転送するように構成できます。

私は個人的に、これは少しやり過ぎだと思っており、hosts ファイルが機能しない理由がわかりません。しかし、それはあなたが抱えている問題を解決するはずです。通常の DNS サーバーもフォワーダーとして設定してください。

于 2010-08-10T00:24:20.360 に答える
2

dev.yourdomain.comを指す実際の完全修飾ドメイン名 ( など) を使用するか127.0.0.1、適切なホスト ファイル (通常 *nix 環境では /etc/hosts) を編集してみてください。

于 2010-08-02T18:20:12.387 に答える
1

へのリクエストの作成

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
于 2010-08-11T15:40:23.637 に答える
1

サーバーは実際にリクエストを受け取りますか? また、ホスト名 (エイリアス) を適切に処理していますか?

.hosts ファイルに追加した後

ウェブサーバーのログをチェックして、リクエストがどのように受信されたかを確認してください...

curl には、送信されたリクエストと受信したレスポンスをダンプするオプションがあります。これは trace と呼ばれ、ファイルに保存されます。

- 痕跡

ホストまたはヘッダー情報が欠落している場合は、構成オプションを使用してそれらのヘッダーを強制できます。

コマンドラインでcurlリクエストが機能するようにしてから、PHPで実装しようとします。

設定オプションは

-K/--config

curl に関連するオプションはこちら

--trace 指定された出力ファイルへの、説明情報を含むすべての着信および発信データの完全なトレース ダンプを有効にします。出力を stdout に送信するには、ファイル名として「-」を使用します。

      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 引数を読み取る構成ファイルを指定します。構成ファイルは、実際のコマンド ラインに記述されているかのように使用されるコマンド ライン引数を書き込むことができるテキスト ファイルです。オプションとそのパラメータは、空白、コロン、等号、またはそれらの任意の組み合わせで区切って、同じ構成ファイル行で指定する必要があります (ただし、推奨される区切り文字は等号です)。パラメータに空白を含める場合は、パラメータを引用符で囲む必要があります。二重引用符内では、次のエスケープ シーケンスを使用できます: \、\"、\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.
于 2010-08-08T19:46:30.497 に答える