Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php simplexml_是否使用代理服务器加载文件()?怎样?_Php_Curl_Proxy_Simplexml - Fatal编程技术网

Php simplexml_是否使用代理服务器加载文件()?怎样?

Php simplexml_是否使用代理服务器加载文件()?怎样?,php,curl,proxy,simplexml,Php,Curl,Proxy,Simplexml,我尝试使用代理在php中实现以下代码: $data = simplexml_load_file('http://www.testdomain.com/data/search?q='. urlencode($searchstring).'&format=xml'); 如何修改此代码,以便通过代理服务器获取URL? 我找到了一些例子,但它们都使用cURL,我不知道如何实现它 任何帮助都将不胜感激 注释: 这样行吗 $url = "h

我尝试使用代理在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\u load\u字符串中。

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

$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."" );

谢谢我改变了我原来的帖子-它会像我的例子中那样工作吗?除了第1行中的打字错误,它应该是。你没测试过吗?对不起,现在不能在这台计算机上测试,必须等到今晚。。。什么错字?好的。第一行:
$url=”http://www.testdomain.com/data/search?q=“.urlencode($searchstring)。”&format=xml”而不是
$url=”http://www.testdomain.com/data/search?q=“.urlencode($searchstring)。”&format=xml”-有一个流上下文,允许您指定代理服务器:-HTTP的代理(和其他)流上下文选项: