Home >Backend Development >PHP Tutorial >file_get_contents cannot obtain web page content

file_get_contents cannot obtain web page content

WBOY
WBOYOriginal
2016-07-29 09:04:271245browse

In the process of signature verification, the server often needs to obtain a user's information from the channel server. There are generally two methods, curl and file_get_contents.

Under normal circumstances, there will be no problem if you use it like this.

<span> 1</span><span>public</span><span>function</span> OauthPostExecuteNew(<span>$sign</span>,<span>$requestString</span>,<span>$request_serverUrl</span><span>){
</span><span> 2</span><span>$opt</span> = <span>array</span>("http"=><span>array</span><span>(
</span><span> 3</span>         "method"=>"GET",
<span> 4</span>         "header"=><span>array</span>("param:".<span>$requestString</span>,"oauthsignature:".<span>$sign</span>),
<span> 5</span>         "request_fulluri"=><span>true</span><span> 6</span><span>        )
</span><span> 7</span><span>    );
</span><span> 8</span><span> 9</span><span>$context</span> = <span>stream_context_create</span>(<span>$opt</span><span>);
</span><span>10</span><span>$res</span>=<span>file_get_contents</span>(<span>$request_serverUrl</span>, <span>false</span>, <span>$context</span><span>);
</span><span>11</span><span>12</span><span>return</span><span>$res</span><span>;
</span><span>13</span> }

However, since our server connects to the external network through a proxy, you need to bring the proxy parameter when using stream_context_create to access the channel server.

So add "proxy"=>$proxy field to the $opt array in the above code. After adding it, I found that file_get_contents still cannot be verified normally.

I was puzzled, so I went to the official website to check file_get_contents and found that there was no explanation about proxy. Then I searched for stream_context_create. The official explanation has this sentence

params

must be $arr['parameter'] = Associative array in $value format. Please refer to the standard resource flow parameter list in context parameters.

Then let’s go into context_parameters to view the parameter configuration. Because we are using HTTP, check the HTTP context

to view the proxy server address specified by the proxy-related

proxystring

URI. (e.g. tcp://proxy.example.com:5100).

request_fulluri boolean

When set to TRUE , the entire URI will be used when constructing the request. (i.e. GET http://www.example.com/path/to/file.html HTTP/1.0). Although this is a non-standard request format, some proxy servers require it.

The default value is FALSE.

I found that only proxy was configured, but request_fulluri was not configured, so I added request_fulluri=true and the verification passed.

Note: When using the proxy parameter, you need to change http to tcp. I don’t know the specific reason. I'll update here when I find out.

The above introduces the problem that file_get_contents cannot obtain web content, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn