【问题标题】:file_get_contents with https requests via proxy通过代理使用 https 请求的 file_get_contents
【发布时间】:2015-10-16 13:08:08
【问题描述】:

如何通过代理服务器进行 HTTPS 请求

代理服务器是 debian 上的tinyproxy

代码

$context = stream_context_create([
    'http' => [
        'proxy' => 'tcp://xx.xx.xx.xx:8888',
        'request_fulluri' => true
    ]
]);

echo file_get_contents('https://www.google.com', false, $context);

错误

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\wamp\www\google\test.php on line 10

Warning: file_get_contents(https://www.google.com): failed to open stream: Cannot connect to HTTPS server through proxy in C:\wamp\www\google\test.php on line 10

【问题讨论】:

    标签: php https proxy fsockopen


    【解决方案1】:

    我建议使用 cURL 来执行此操作。在 stackoverflow 的某个地方,一位用户这么说,我完全同意。

    file_get_contents() 是一个简单的螺丝刀。非常适合简单的 GET 请求头,HTTP请求方法,超时,cookiejar, 重定向,其他重要的事情无关紧要。带有 setopt 的 cURL 是一个几乎所有你能想到的选项的电钻。

    <?php
    
    $url = 'https://www.google.com';
    // to check your proxy
    // $url = 'http://whatismyipaddress.com/';
    $proxy = '50.115.194.97:8080';
    
    // create curl resource
    $ch = curl_init();
    
    // set options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https-url/31164409#31164409
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    
    // $output contains the output string
    $output = curl_exec($ch);
    
    // close curl resource to free up system resources
    curl_close($ch); 
    
    echo $output;
    
    ?>
    

    【讨论】:

    【解决方案2】:

    可以通过将 stream_context_create 中的“http”更改为“https”来解决导致第二次警告的问题

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 2014-08-25
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多