公司无法访问外网,必须使用代理,这样file_get_contents函数就无效了,怎么才能让php使用代理呢?

解决方案 »

  1.   

    curlCURLOPT_HTTPPROXYTUNNEL 代理
      

  2.   

    同意楼上的
    http://php.net/manual/en/function.file-get-contents.php
    file-get-contents只支持
    filename,use_include_path,offset,maxlen。
    使用代理,可以换用curl
      

  3.   

    使用curl库通过代理服务器访问网页   
    <?php
    function curl_string ($url,$user_agent,$proxy){$ch = curl_init();
    curl_setopt ($ch, CURLOPT_PROXY, $proxy);
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
    curl_setopt ($ch, CURLOPT_HEADER, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
    $result = curl_exec ($ch);
    curl_close($ch);
    return $result;}$url_page = "http://www.366edu.net";
    $user_agent = "Mozilla/4.0";
    $proxy = "http://192.11.222.124:8000";
    $string = curl_string($url_page,$user_agent,$proxy);
    echo $string;
    ?>
      

  4.   

    file_get_contents 也可以通过proxyhttp://stackoverflow.com/questions/1336262/file-get-contents-behind-a-proxy
      

  5.   

    $aContext = array(
        'http' => array(
            'proxy' => 'tcp://192.168.0.2:3128',
            'request_fulluri' => true,
        ),
    );
    $cxContext = stream_context_create($aContext);$sFile = file_get_contents("http://www.google.com", False, $cxContext);echo $sFile;
      

  6.   

    来画个蛇添个足
    PHP stream_context_create()作用和用法分析创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
    作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
    函数原型:resource stream_context_create ([ array $options [, array $params ]] ) $url = "http://www.test.com/";
    $ctx = stream_context_create(array(
    'http' => array('timeout' => 5,
    'proxy' => 'tcp://127.0.0.8:8080',
    'request_fulluri' => True,)
    )
    );
    $result = file_get_contents($url, False, $ctx);
    echo $result;