로컬 가상 호스트를 사용하도록 cURL 설정
Apache 또는 Ngnix를 사용하여 항상 실제 프로젝트를 기반으로 개발 사이트를 만듭니다. 예를 들어 파일에 파일 http://project1.loc
을 추가 한 후 .hosts
브라우저 사용에 아무런 문제가 없습니다.
그러나 http://project1.loc/post.json
동일한 URL에 cURL 요청 ( )을 시도 하면 시간 초과 외에는 아무것도 얻지 못합니다. cURL이 내 사용자 정의 호스트를 신경 쓰지 않고 정보 서버로 직접 이동한다고 가정합니다.
이 문제를 어떻게 해결할 수 있습니까?
업데이트 사용자 정의 헤더 "HOST : http : //project1.loc "을 설정했는데 이제 400 오류가 발생하지만 순간적으로 cURL이 적어도 hosts 파일을 사용하고 있다고 가정합니다 ...
실제로 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
인증서가 127.0.0.1
URL 의 호스트 이름 과 일치하지 않기 때문에 실패 합니다.
두 번째 예는 HTTPS에서 올바르게 작동합니다.
본질적으로 "Host"헤더를 전달하면 호스트 -H
가 헤더 세트에 해킹되지만 curl의 모든 호스트 특정 인텔리전스는 무시됩니다. 를 사용하면 --resolve
적용되는 모든 일반 논리를 활용하지만 단순히 DNS 조회가 명령 줄 옵션에서 데이터를 반환 한 것처럼 가장합니다. 그것은처럼 작동 /etc/hosts
합니다.
참고 --resolve
는 포트 번호를 사용하므로 HTTPS의 경우
curl --resolve 'yada.com:443:127.0.0.1' https://yada.com/something
편집 : 이것은 현재 받아 들여지는 대답이지만 독자는 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
헤더에 넣지 마십시오 .)
올바른 호스트 파일 (일반적으로 * nix 환경의 / etc / hosts dev.yourdomain.com
)을 가리 키 127.0.0.1
거나 수정하는 완전한 정규화 된 도메인 이름 (예 :)을 사용하십시오.
이것은 드문 문제가 아닌 것 같습니다.
이것을 먼저 확인하십시오 .
그래도 도움이되지 않으면 Windows에 로컬 DNS 서버를 설치할 수 있습니다 (예 : this) . localhost를 DNS 서버로 사용하도록 Windows를 구성하십시오. 이 서버는 필요한 모든 가짜 도메인에 대해 신뢰할 수 있고 다른 모든 요청에 대해 실제 DNS 서버로 요청을 전달하도록 구성 할 수 있습니다.
나는 개인적으로 이것이 약간 위에 있다고 생각하며 hosts 파일이 작동하지 않는 이유를 알 수 없습니다. 그러나 그것은 당신이 겪고있는 문제를 해결해야합니다. 일반 DNS 서버도 전달자로 설정해야합니다.
서버가 실제로 요청을 받고 호스트 이름 (별칭)을 올바르게 처리하고 있습니까?
내 .hosts 파일에 추가 한 후
요청이 어떻게 들어 왔는지 확인하려면 웹 서버 로그를 확인하십시오.
curl에는 전송 된 요청과 응답 수신을 덤프하는 옵션이 있으며이를 추적 (trace)이라고하며 파일에 저장됩니다.
--자취
호스트 또는 헤더 정보가 누락 된 경우 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 Specify which config file to read curl arguments from. The config file is a text file in which command line arguments can be written which then will be used as if they were written on the actual command line. Options and their parameters must be specified on the same config file line, separated by whitespace, colon, the equals sign or any combination thereof (however, the preferred separa- tor is the equals sign). If the parameter is to contain whitespace, the parameter must be enclosed within quotes. Within double quotes, the following escape sequences are available: \, \", \t, \n, \r and \v. A backslash preceding any other letter is ignored. If the first column of a config line is a '#' character, the rest of the line will be treated as a comment. Only write one option per physical line in the config 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.
Making a request to
C:\wnmp\curl>curl.exe --trace-ascii -H 'project1.loc' -d "uuid=d99a49d846d5ae570
667a00825373a7b5ae8e8e2" http://project1.loc/Users/getSettings.xml
Resulted in the -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:
# 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
참고URL : https://stackoverflow.com/questions/3390549/set-curl-to-use-local-virtual-hosts
'IT박스' 카테고리의 다른 글
git에서 풀 버전 트리보기 (0) | 2020.08.06 |
---|---|
pecl 확장을 설치하려고 할 때 mac OS x 10.7.3의 $ PHP_AUTOCONF 오류 (0) | 2020.08.06 |
동 기적으로 포트를 읽거나 쓸 때 재귀를 피합니까? (0) | 2020.08.06 |
clang으로 더 빠른 코드 완성 (0) | 2020.08.06 |
Malloc vs New – 다른 패딩 (0) | 2020.08.06 |