【问题标题】:How to use file_get_contents with proxy?如何将 file_get_contents 与代理一起使用?
【发布时间】:2019-01-30 17:09:37
【问题描述】:

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

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

警告:file_get_contents(https://www.instagram.com/explore/tags/test/):无法打开流:无法通过 D:\XAMPP\htdocs\instahu\app\vendor\classes\instagram.php 第 154 行中的代理连接到 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]];
}

【问题讨论】:

标签: php ssl proxies


【解决方案1】:

对不起,伙计们,我在我的案例中找到了一个解决方案,我所做的所有更改都是在 $aContext 数组中将键 'http' 更改为 'https' 并且它可以工作

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2016-05-06
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多