-1

我尝试在 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);

?

4

2 回答 2

2

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

于 2013-09-06T09:03:47.917 回答
0

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

$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."" );
于 2018-11-20T09:08:28.887 回答