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