php - 通过 ssl 连接到 google.com

标签 php ssl

我正在使用这个代码

$sock = fsockopen("ssl://google.com", 443, $errno, $errstr, 30); 
if (!$sock) die("$errstr ($errno)\n"); 
fputs($sock, "GET / HTTP/1.0\r\n"); 
fputs($sock, "Host: google.com\r\n"); 
fputs($sock, "\r\n"); 
$headers = ""; 
while ($str = trim(fgets($sock, 4096))) 
$headers .= "$str\n"; 
$body = ""; 
while (!feof($sock)) 
$body.= fgets($sock, 4096); 
fclose($sock); 
echo '<div>'.$headers.'</div>'; 
echo '<div>'.$body.'<div>'; 

我收到这样的页面:

HTTP/1.0 301 Moved Permanently Location: https://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Thu, 12 Dec 2013 11:03:07 GMT Expires: Sat, 11 Jan 2014 11:03:07 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 220 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Alternate-Protocol: 443:quic 301 Moved

The document has moved here.

我哪里错了?为什么我需要使用 ssl?我可以只写https吗?如果我写 https,那么我会收到错误消息。

最佳答案

您正在尝试通过根域“google.com”访问该网站。服务器正在尝试将您重定向到实际包含该站点的子域:“www.google.com。添加“www.”子域前缀,您将不会再获得 301 重定向。

关于php - 通过 ssl 连接到 google.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20541810/

相关文章:

docker - 如何通过 Nginx 将外部请求的端口 80 和 443 分别转发到端口 81 和 444?

java - 客户端无法使用自签名证书连接到 REST API

php - Laravel 仅在不为空时更新

php - WordPress 按日期输入类型查询帖子

php - 从命令行执行 php 和 crontab 时权限被拒绝

php - 在 Redis 中,您将如何更新 key 并重置原始 TTL?

java - 为 Tomcat 7 配置 SSL 导致 ERR_SSL_VERSION_OR_CIPHER_MISMATCH 错误

java ssl 多客户端证书

php - 更改基本网址以允许相对链接

php - 无法使用 ssl 与 PDO 连接,但 mysqli 与 ssl 可以工作