您好,欢迎访问一九零五行业门户网

file_get_contents 获取不了网页内容

服务器在做验签的过程中,经常需要向渠道服务器获取某个用户的信息。一般有两种方法,curl和file_get_contents。
一般情况下,像这样用,不会有问题。
1publicfunction oauthpostexecutenew($sign,$requeststring,$request_serverurl){ 2$opt = array(http=>array( 3 method=>get, 4 header=>array(param:.$requeststring,oauthsignature:.$sign), 5 request_fulluri=>true 6 ) 7 ); 8 9$context = stream_context_create($opt);10$res=file_get_contents($request_serverurl, false, $context);1112return$res;13 }
但是由于我司服务器连外网时通过代理,所以在使用stream_context_create时需要带上proxy参数,才能访问到渠道的服务器。
所以在上面代码 $opt 数组中带上proxy=>$proxy字段。加上之后发现file_get_contents仍然不能正常验签。
百思不解,遂到官网上来查查file_get_contents,发现并没有关于proxy的解释,然后搜stream_context_create,官方解释有这句话
params
必须是 $arr['parameter'] = $value 格式的关联数组。 请参考 context parameters 里的标准资源流参数列表。
那么 我们进入context_parameters 查看参数配置。因为我们使用的是http方式,所以查看http context
查看跟proxy相关的
proxy string
uri 指定的代理服务器的地址。(e.g. tcp://proxy.example.com:5100).
request_fulluri boolean
当设置为 true 时,在构建请求时将使用整个 uri 。(i.e. get http://www.example.com/path/to/file.html http/1.0)。 虽然这是一个非标准的请求格式,但某些代理服务器需要它。
默认值是 false.
发现只配置了proxy,而并没有配置request_fulluri,遂加上request_fulluri=true,验证通过。
注意:使用proxy参数时需要把http 改为tcp 具体什么原因,不知道。等我查到了再到这里更新。
以上就介绍了file_get_contents 获取不了网页内容,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息