我正在使用这个代码
$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/