怎么通过代理来使用file_get_contents?

phf0313 2011-09-30 05:34:47
公司无法访问外网,必须使用代理,这样file_get_contents函数就无效了,怎么才能让php使用代理呢?
...全文
245 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyaspnet 2011-10-01
  • 打赏
  • 举报
回复
使用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;
?>
mumubangditu 2011-10-01
  • 打赏
  • 举报
回复
同意楼上的
http://php.net/manual/en/function.file-get-contents.php
file-get-contents只支持
filename,use_include_path,offset,maxlen。
使用代理,可以换用curl
黄袍披身 2011-10-01
  • 打赏
  • 举报
回复
来画个蛇添个足
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;
xuzuning 2011-10-01
  • 打赏
  • 举报
回复
$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;
helloyou0 2011-10-01
  • 打赏
  • 举报
回复
file_get_contents 也可以通过proxy

http://stackoverflow.com/questions/1336262/file-get-contents-behind-a-proxy
ohmygirl 2011-09-30
  • 打赏
  • 举报
回复
curl

CURLOPT_HTTPPROXYTUNNEL 代理

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧