PHP에서 curl 사용시 디버깅을 위해 verbose 옵션을 세팅하고 결과값을 받아오는 법.

$c = curl_init('https://www.google.com');
 
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
 
// -v
curl_setopt($c, CURLOPT_VERBOSE, true);
// CURLOPT_VERBOSE 세팅시 STDERR로 출력되는데 이걸 임시로 만든 스트림으로 전환
$verboseStream = fopen('php://memory', 'w+');
curl_setopt($c, CURLOPT_STDERR, $verboseStream);
 
curl_exec($c);
 
// 출력
rewind($verboseStream);
echo stream_get_contents($verboseStream);
 
fclose($verboseStream);
curl_close($c);

 

아래와 같은 출력 결과를 얻을 수 있음.

* 로 시작하는 건 curl에서 제공되는 추가 정보
> 로 시작하는 건 request header
< 로 시작하는 건 response header

* About to connect() to www.google.com port 443 (#0)
*   Trying 216.58.220.100... * connected
* Connected to www.google.com (216.58.220.100) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=www.google.com,O=Google LLC,L=Mountain View,ST=California,C=US
*       start date: Jul 15 08:34:33 2020 GMT
*       expire date: Oct 07 08:34:33 2020 GMT
*       common name: www.google.com
*       issuer: CN=GTS CA 1O1,O=Google Trust Services,C=US
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
 
< HTTP/1.1 200 OK
< Date: Tue, 11 Aug 2020 03:40:36 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2020-08-11-03; expires=Thu, 10-Sep-2020 03:40:36 GMT; path=/; domain=.google.com; Secure
< Set-Cookie: NID=204=ZnZJMun67olpxRGoZVvFJrOo9x794GjSc0_wFKv3r7EGMF2H0iUMDpd5PVYnBaQYj1rsJglgBTqJpyMhrkXycQQGOpMs5UJIvjXxTat1wXFjDM9nIAZknit1lS8NO37dPUe9e366CSaK4o1X74YmO-Zdwa-xZHxp_cTydHQSGvE; expires=Wed, 10-Feb-2021 03:40:36 GMT; path=/; domain=.google.com; HttpOnly
< Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
* Connection #0 to host www.google.com left intact

 

 

 

 

.

Posted by bloodguy
,