使用file_get_contents函数时通过代理

时间:2012-02-21 11:29:32

标签: php

我在代理上运行,当我使用函数file_get_contents时,我得到错误,如

Warning: file_get_contents(http://www.abc.com/) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\Kurogo123\lib\DataController.php on line 520

我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要创建一个流上下文,以便超越代理服务器。试试这个:

// Define a context for HTTP.
$aContext = array(
     'http' => array(
         'proxy' => 'tcp://127.0.0.1:8080', // This needs to be the server and the port of the Proxy Server.
         'request_fulluri' => true,
         ),
     );
$cxContext = stream_context_create($aContext);

// Now all file stream functions can use this context.

$sFile = file_get_contents("http://www.example.com", false, $cxContext);

 echo $sFile;