simplexml_load_file() 与代理服务器 ?如何


simplexml_load_file() with proxy server ? how?

我尝试在 php 中实现以下代码并使用代理:

$data = simplexml_load_file('http://www.testdomain.com/data/search?q='.
                             urlencode($searchstring).'&format=xml');

如何修改此代码,以便通过代理服务器获取 URL?我找到了一些例子,但它们都使用 cURL,我不完全知道如何实现它。

任何帮助将不胜感激!

注解:它会这样工作吗?

$url = "http://www.testdomain.com/data/search?q='.urlencode($searchstring).'&format=xml";
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) 
        AppleWebKit/532.4 (KHTML, like Gecko) 
        Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, '202.95.141.129:8080');
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$curldata = curl_exec($ch);
curl_close($ch);

$data = simplexml_load_string($curldata);

通过 cURL 获取文件并在simplexml_load_string中加载内容。

这是一些示例代码。它对我有用:

$context = array(
  'http' => array(
    'proxy' => 'proxy.domain:3128',
    'request_fulluri' => true,
  ),
);
$cxContext = stream_context_create($context);
$sFile = file_get_contents("http://static.cricinfo.com/rss/livescores.xml", False, $cxContext);
$xml = simplexml_load_string ( $sFile."" );