2012-07-01 3 views
1

내가하고 싶은 것은 아약스와 함께 위키 백과에서 데이터를 검색하는 것입니다. 나중에 클라이언트 쪽 스크립트를 남겨두고 임의의 콘텐츠를 검색하려고했습니다. fopen() 및 fread() 메서드로 시도했지만 작동하지 않았고 프록시를 사용하는 인터넷 공급자 코드가있는 기사를 보았습니다. 이후로 나는 아래 코드를 시도했지만 어떤 응답도하지 않았다. 내가 코드를 시도 있도록 확인도메인 간 데이터 검색 PHP

<?php 
$opts = array('http' => array('proxy' => 'tcp://10.10.10.101:8080', 'request_fulluri' => true)); 
$context = stream_context_create ($opts); 
$data = file_get_contents('http://www.php.net', false, $context); 
echo $data; 
?> 

적절한 프록시 값으로 제안 :

<?php 


$url = 'http://www.php.net'; 
$proxy = '10.10.10.101:8080'; 
//$proxyauth = 'user:password'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$curl_scraped_page = curl_exec($ch); 
curl_close($ch); 

echo $curl_scraped_page; 

그러나 그것은 나에게이 오류 제공합니다 : HTTP/1.0 403 금지 날짜 : 2012년 (월) 9시 41분 7월 2일을 : 20 GMT 서버 : Apache Content-Type : text/plain 금지 된 대상 호스트

왜 작동하지 않는지 그리고 어떻게 문제를 해결할 수 있는지 얻지 못합니다.

답변

2

브라우저가 아닌 서버에서 데이터를로드하기 때문에 실제로 크로스 도메인 문제가 아닙니다.

프록시를 통해 PHP에서 웹 페이지를로드하려면 cURL (PHP http 클라이언트 : http://php.net/manual/en/book.curl.php)을 사용하는 것이 가장 좋습니다. 여기

은 예입니다 -이 비슷한 질문 (http://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy)에서 가져온 것입니다 :

<?php 


$url = 'http://www.php.net'; 
$proxy = '10.10.10.101:8080'; 
//$proxyauth = 'user:password'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$curl_scraped_page = curl_exec($ch); 
curl_close($ch); 

echo $curl_scraped_page; 

프록시에서 인증이 필요한 경우 - $ proxyauth var ...

+0

시도했지만 403 오류가 출력됩니다. HTTP/1.0 403 금지됨 날짜 : 2012 년 7 월 2 일 09:41:20 GMT 서버 : Apache Content-Type : text/plain 대상 호스트 금지 – BigCola

2

방금 ​​자신의 프록시 주소를 사용하여 코드를 테스트 한 결과 작동합니다.

<?php 
    $url = 'http://www.php.net'; 
    $proxy = '192.168.4.200:3128'; 
    //$proxyauth = 'user:password'; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_PROXY, $proxy); 
    //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $curl_scraped_page = curl_exec($ch); 
    curl_close($ch); 

    echo $curl_scraped_page; 
?> 

그래서, 당신이보고있는 것은 아마 허용하지 않는 프록시 자체이다 (일부를 - 또는 모든 외부) 사이트에 도달 할 수 있습니다. 프록시로 인증하는 것만으로도 충분할 것입니다.

이것은 네트워크 관리자와의 대화를 취소 할 때까지는 get_contents, curl, fsockopen 또는 다른 방법으로는 수행 할 수 없음을 의미합니다.