2014-04-23 1 views
2

PHP 사용 웹 사이트 페이지를 크롤링하고 자동으로 이미지를 가져 오려고합니다. Internal Server ErrorPHP 500 내부 서버 오류 file_get_contents

<?php 
$url = "http://www.domain.co.uk/news/local-news"; 

$str = file_get_contents($url); 
?> 

<?php 
    $opts = array('http'=>array('header' => "User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1\r\n")); 
    $context = stream_context_create($opts); 
    $header = file_get_contents('http://www.domain.co.uk/news/local-news',false,$context); 
?> 

또한

<?php 
include('simple_html_dom.php'); 

$html = file_get_html('http://www.domain.co.uk/news/local-news'); 

$result = $html->find('section article img', 0)->outertext; 
?> 

그러나이 모든 반환 :

나는 다음과 같은 시도했습니다. 나는 브라우저에서 사이트를 완벽하게 볼 수 있지만 PHP에서 페이지를 잡으려고 할 때 실패합니다.

시도할만한 것이 있습니까?

+1

[오류보고 활성화] (http : //blog.flowl.info/2013/enable-display-php-errors/) – DanFromGermany

+0

가능한 복제본 [PHP 파일 \ _get \ _contents 500 내부 서버 오류] /stackoverflow.com/questions/10524748/php-file-get-contents-500-internal-server-error) – majidarif

답변

2

아래 코드를 시도하십시오. 콘텐츠를 로컬 파일에 저장합니다.

<?php 
$ch = curl_init("http://www.domain.co.uk/news/local-news"); 
$fp = fopen("localfile.html", "w"); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); 
fclose($fp); 
?> 

이제 localfile.html을 준비 할 수 있습니다.

+0

파일을 성공적으로 만들지 만 코드에 다음을 추가하여 액세스하려고하면 localfile을 덮어 씁니다 .html 및 500 오류를 반환 'include ('simple_html_dom.dom.php'); $ html = file_get_html ('http://domain.com/build/wp-content/plugins/news-plugin/localfile.html'); $ result = $ html-> find ('. lead-story', 0) -> outertext; echo $ result;' – ngplayground

+0

제게 잘 작동합니다 .. 코드를 확인하십시오 .. –

1

때때로 file_get_contents를 사용하여 http URL을 여는 중 오류가 발생할 수 있습니다. 당신이 나를 위해 php.ini의

allow_url_fopen = On을 설정 한 경우에도 또한이 솔루션은 뭔가 "USER_AGENT"로 설정하는 것이 었습니다.

+0

더 나은 점은 cUrl을 사용하는 것입니다. 많은 호스트가 file_get_contents의 사용을 차단합니다. –