如何在代理中使用file_get_contents?

时间:2018-08-24 08:50:11

标签: php ssl proxies

当我将file_get_contents与SSL和代理一起使用时,出现以下错误:

  

警告:file_get_contents():SSL操作失败,代码为1。OpenSSL错误消息:error:1408F10B:SSL例程:ssl3_get_record:D:\ XAMPP \ htdocs \ instahu \ app \ vendor \ classes \ instagram中的版本号错误。 php 154行

     

警告:file_get_contents(https://www.instagram.com/explore/tags/test/):无法打开流:无法通过154行上D:\ XAMPP \ htdocs \ instahu \ app \ vendor \ classes \ instagram.php中的代理连接到HTTPS服务器

protected function curl_send($url,$post = null,$headers = null) {

    global $settings;
    global $language;
    global $lng;
    global $currentUrl;

    $file = file_get_contents("http://" . $_SERVER['HTTP_HOST'] . "/app/vendor/classes/proxy.txt");

    $file = explode("", $file);
    if (count($file) == 0) {

        include_once __DIR__.'/../../templates/500.php';
        exit();
    }

    $mysqli = new mysqli($settings['db']['host'],
                     $settings['db']['user'],
                     $settings['db']['pass'],
                     $settings['db']['name']);
    $exe_proxies = $mysqli->query('SELECT * FROM `proxy_used`');
    $proxies = [];
    while ($row = $exe_proxies->fetch_assoc()) {
        $proxies[] = $row['proxy'];
    }
    foreach ($file as $proxy) {
        if (!in_array($proxy, $proxies)) {
            $target_proxy = $proxy;
            break;
        }
    }

    if (!isset($target_proxy)) {
        $mysqli->query('DELETE FROM `proxy_used`');
        $target_proxy = $file[0];
    }
    $mysqli->query('INSERT INTO `proxy_used` SET `proxy` = "' . $target_proxy . '"');
    $target_proxy = 'tcp://'.$target_proxy;
    $mysqli->close();
    $headers['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36';

    if ($headers) {
        foreach ($headers as $key => $header) {
            $result[] = $key . ":" . $header;
        }
        $headers = join("\r\n", $result);
    }

    $aContext = [
      'http' => [
        'proxy' => $proxy,
        'request_fulluri' => true,
        //'method' => $post ? 'POST' : 'GET',
        //'content' => http_build_query($post),
        'header' => $headers,
      ],
    ];

    $cxContext = stream_context_create($aContext);

    $server_output = file_get_contents($url, False, $cxContext);
    preg_match('#\d{3}#', $http_response_header[0], $code);

    //var_dump($server_output, $code[0], $aContext, $url); exit();

    return ['server_output' => $server_output, 'httpcode' => $code[0]];
}

1 个答案:

答案 0 :(得分:0)

对不起,我找到了一个解决方案,我所做的全部更改就是将$ aContext数组中的更改键'http'更改为'https',并且可以正常工作