通过php从外部https路径读取

时间:2015-08-02 05:32:33

标签: php sockets https file-get-contents fsockopen

我想从外部网站上读取一些协议为https的短语。 我已经通过此代码为具有http协议的网站执行此操作:

$homepage  = file_get_contents('https://www.examlple.com/');
echo $homepage;

但它不适用于https网站。然后我用了这个:

$fp = fsockopen("https://www.example.com/", 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "Data sent by socket");
    $content = "";
    while (!feof($fp)) {  //This looped forever
        $content .= fread($fp, 1024);
    }
   fclose($fp);
   echo $content;
}

但我总是收到错误:

  

无法找到套接字传输“http” - 您是否忘记启用   它配置PHP时? (2)

实际情况是从分析中获取我网站的统计数据。

1 个答案:

答案 0 :(得分:0)

Fsockopen不知道&#34; http,https&#34;。请从您的域中删除http部分并使用SSL端口进行连接。

参见示例,这应该有效:

// open ssl connection - dont add "http or https!"
$host = "ssl://" . "example.com";
$port = 443;
$fp = fsockopen($host,$port);

if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "Data sent by socket");
    $content = "";
    while (!feof($fp)) {  //This looped forever
        $content .= fread($fp, 1024);
    }
   fclose($fp);
   echo $content;
}