我尝试在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);
答案 0 :(得分:2)
通过cURL获取文件并在simplexml_load_string中加载内容。
答案 1 :(得分:0)
这是一些示例代码。它对我有用:
class MyClass {
public function functionOne(User $user) {
$user->startProcess(function ($data) {
$this->makeUpdate($data);
});
}
protected function makeUpdate($data)
{
//... do something
}
}
$myClass = new MyClass();
$myClass->functionOne($user);