【问题标题】:reading from external https path by php通过 php 从外部 https 路径读取
【发布时间】:2015-10-24 11:21:36
【问题描述】:

我想从使用 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)

实际上是从分析中获取我网站的统计信息。

【问题讨论】:

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


    【解决方案1】:

    Fsockopen 不知道“http、https”。请从您的域中删除 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;
    }
    

    【讨论】:

    • 错误刚刚变成:无法找到套接字传输“ssl” - 您在配置 PHP 时是否忘记启用它? (160249520)
    • 只需在 php.ini 中取消注释 extension=php_openssl.dll 并重启服务器
    • 我的网站是本地测试。我做到了并重新启动了 xampp,结果没有任何变化!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多