web-dev-qa-db-ja.com

ローカル仮想ホストを使用するようにcURLを設定します

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

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

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

UPDATEカスタムヘッダー「Host: http://project1.loc 」を設定すると、400になりますエラー-しかし、それらは瞬時なので、cURLは少なくともhostsファイルを使用していると仮定しています...

105
Xeoncross

実際、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ホスト名と一致しないため、上記の最初の例は失敗します。

2番目の例はHTTPSで正しく機能します。

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

--resolveはポート番号を取るため、HTTPSの場合は

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

387
John Hart

EDIT:これは現在受け入れられている回答ですが、読者は this other answer by user John Hart =ニーズにさらに適合。ユーザー Ken によると、バージョン7.21.3で導入されたオプションを使用します(これは 2010年12月にリリースされました 、つまりこの最初の回答の後です)。


編集した質問では、ホスト名としてURLを使用していますが、ホスト名のみである必要があります。

試してください:

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

ここで、project1.locjustホスト名であり、127.0.0.1はターゲットIPアドレスです。

(コマンドラインではなくライブラリのcurlを使用している場合は、Hostヘッダーにhttp://を入れないでください。)

120
Bruno

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

2
Oli

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

this を最初に確認してください。

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

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

2
Matt

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

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

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

curlには、送信された要求と受信された応答をダンプするオプションがあり、トレースと呼ばれ、ファイルに保存されます。

- トレース

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

コマンドラインで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。他の文字の前のバックスラッシュは無視されます。設定行の最初の列が「#」の場合構成ファイルの物理行ごとに1つのオプションのみを記述します。

      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.
1
George Lambert

リクエストをする

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
1
Xeoncross