2013-09-06 2 views
-1

내가 사용하는 프록시와 함께 PHP에서 다음 코드를 실행하려고 :프록시 서버가있는 simplexml_load_file()? 방법?

$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

http://php.net/simplexml_load_file - 프록시 서버를 지정할 수있는 스트림 컨텍스트가 있습니다. http://php.net/libxml_set_streams_context - HTTP에 대한 프록시 및 기타 스트림 컨텍스트 옵션 : http : //php.net/context.http – hakre

답변

2

cURL을 통해 파일을 가져 와서 내용을 simplexml_load_string에로드하십시오.

+0

감사합니다! 내 원래 게시물을 변경 - 내 예제에서와 같이 작동합니까? – user2753657

+1

1 번 줄의 오타를 제외하고는. 테스트하지 않았습니까? – secelite

+0

죄송합니다. 지금이 컴퓨터에서 테스트 할 수 없습니다. 오늘 밤까지 기다려야합니다 ... 오타가 무엇입니까? – user2753657

관련 문제