以下のコードでPHPのcURL関数を使ってYouTubeにGETリクエストを送ろうとしています。
PHP
1 $ch = curl_init($url); 2 3 curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); 4 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 5 curl_setopt($ch, CURLOPT_HEADER, 0); 6 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file); 7 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file); 8 9 if ($this->proxy) { 10 curl_setopt($ch, CURLOPT_PROXY, $this->proxy); 11 } 12 13 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 14 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 15 16 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 17 18 // デバッグ用 19 curl_setopt($ch, CURLOPT_VERBOSE, 1); 20 $verbose = fopen(storage_path()."/logs/tmp", 'w'); 21 curl_setopt($ch, CURLOPT_STDERR, $verbose); 22 23 $result = curl_exec($ch); 24 curl_close($ch); 25 26 return $result;
サーバ上では意図した結果が返ってくるのですが、同じコードを実行してもlocalhostだと違うフォーマットの結果が返ってきます。
コード内のデバッグ用で出力した$chの内容はそれぞれ以下の通りです:
$chの内容@サーバ(PHP:7.2.24、 cURL:7.58.0、 OS: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の内容@Localhost(PHP:7.3.9、 cURL:7.65.1、 OS: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
LocalhostだとHTTP2を使っているようだったので、サーバと同じHTTP1.1を使うようにオプションをつけたりしてみたのですが、やはり返ってくる内容が異なるようです。
サーバと同様の結果をLocalhostでも得たいと思っているのですが、何が問題なのでしょうか?