·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> file_get_contents获取不了网页内容

file_get_contents获取不了网页内容

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23

服务器在做验签的过程中,经常需要向渠道服务器获取某个用户的信息。一般有两种方法,curl和file_get_contents。

一般情况下,像这样用,不会有问题。

 1 public function 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);
11 
12     return $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 具体什么原因,不知道。等我查到了再到这里更新。