Home > Article > Backend Development > file_get_contents cannot obtain web content, filegetcontents_PHP tutorial
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 in the above code, add the "proxy"=>$proxy field to the $opt array. 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 and found this official explanation
<p><code class="parameter">params</code></p> <p class="para">必须是 <em>$arr['parameter'] = $value</em> 格式的关联数组。 请参考 context parameters 里的标准资源流参数列表。</p>
Then we enter context_parameters to view the parameter configuration. Because we are using HTTP, check the HTTP context
View proxy-related
<p class="para"><code class="parameter">proxy</code> <span class="type">string</span></p> <p class="para">URI 指定的代理服务器的地址。(e.g. <em>tcp://proxy.example.com:5100</em>).</p> <p class="para"><code class="parameter">request_fulluri</code> <span class="type">boolean</span></p> <p class="para">当设置为 <code>TRUE</code> 时,在构建请求时将使用整个 URI 。(i.e. <em>GET http://www.example.com/path/to/file.html HTTP/1.0</em>)。 虽然这是一个非标准的请求格式,但某些代理服务器需要它。</p> <p class="para">默认值是 <code>FALSE</code>.</p>
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.