我正在尝试使用php中的curl通过以下代码向YouTube发出GET请求。
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->proxy) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// debug info
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen(storage_path()."/logs/tmp", 'w');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$result = curl_exec($ch);
curl_close($ch);
return $result;
该请求在服务器上按预期工作,但是当我在localhost上运行此代码时,得到的结果不同(格式不同)。
以下是$ ch的输出;
服务器的$ ch(PHP:7.2.24,cURL:7.58.0,Ubuntu)
* Hostname in DNS cache was stale, zapped
* Trying 172.217.175.110...
* TCP_NODELAY set
* Connected to www.youtube.com (172.217.175.110) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.google.com
* start date: Sep 3 06:36:33 2020 GMT
* expire date: Nov 26 06:36:33 2020 GMT
* issuer: C=US; O=Google Trust Services; CN=GTS CA 1O1
* SSL certificate verify ok.
> GET /watch?v=ugRc5jx80yg HTTP/1.1
Host: www.youtube.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: */*
Cookie: GPS=1; VISITOR_INFO1_LIVE=5Q7KVdbZp2U; YSC=0Y4SURxvLO4
< HTTP/1.1 200 OK
< Expires: Tue, 27 Apr 1971 19:44:06 GMT
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Cache-Control: no-cache
< Content-Type: text/html; charset=utf-8
< Strict-Transport-Security: max-age=31536000
< Date: Fri, 25 Sep 2020 19:17:47 GMT
< Server: YouTube Frontend Proxy
< X-XSS-Protection: 0
< Alt-Svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":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.youtube.com left intact
$ ch的本地主机(PHP:7.3.9,cURL:7.65.1,Mac)
* Trying 172.217.26.14:443...
* TCP_NODELAY set
* Connected to www.youtube.com (172.217.26.14) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /Applications/MAMP/Library/OpenSSL/cert.pem
CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.google.com
* start date: Sep 3 06:36:33 2020 GMT
* expire date: Nov 26 06:36:33 2020 GMT
* issuer: C=US; O=Google Trust Services; CN=GTS CA 1O1
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fd730075e00)
> GET /watch?v=UszMjWeprtk HTTP/2
Host: www.youtube.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: */*
Cookie: VISITOR_INFO1_LIVE=J-EVzlugpNc; YSC=zMeI_Rv37Nc
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200
< x-frame-options: SAMEORIGIN
< content-type: text/html; charset=utf-8
< strict-transport-security: max-age=31536000
< x-content-type-options: nosniff
< expires: Mon, 01 Jan 1990 00:00:00 GMT
< date: Fri, 25 Sep 2020 23:07:59 GMT
< pragma: no-cache
< cache-control: no-cache, no-store, max-age=0, must-revalidate
< server: YouTube Frontend Proxy
< x-xss-protection: 0
< alt-svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":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
<
* Connection #0 to host www.youtube.com left intact
我不知道为什么结果格式不同,如何获得与本地主机上的服务器相同的结果?