2011-05-11 2 views
0

내 사이트로드까지 약 45 초가 소요됩니다. 그것은 tumblr에서 일부 XML을 가져오고 있기 때문입니다. 그러나 이것이 내 서버의 잘못, tumblr의 잘못 또는 다른 요인인지는 알 수 없습니다. 이 스크립트를 5 초 만에 타임 아웃시키고 echo 'tumblr is down'할 수 있습니까? 거의 1 분 후에 그냥 시간 초과하는 대신에? 이 코드tumblr에서 가져 오기 위해 simplexml_load_file 사용 - 매번 시간 초과되었습니다.

Warning: simplexml_load_file(http://blog.yaytalent.com/api/read?type=post&start=1&num=2) [function.simplexml-load-file]: failed to open stream: Connection timed out in /nfs/c08/h02/mnt/122191/domains/yaytalent.com/html/index.php on line 86

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://blog.yaytalent.com/api/read?type=post&start=1&num=2" in /nfs/c08/h02/mnt/122191/domains/yaytalent.com/html/index.php on line 86

:

는이 오류를 받고 있어요

이 경우
<?php 
$request_url = "http://blog.yaytalent.com/api/read?type=post&start=1&num=2"; 
$xml = simplexml_load_file($request_url); 
$title = $xml->posts->post->{'regular-title'}; 
$post = $xml->posts->post->{'regular-body'}; 
$link = $xml->posts->post['url']; 
$small_post = substr($post,0,270); 
echo "<h2><a target=frame2 href='".$link."'>$title</a></h2>"; 
echo "<p>$small_post... <a target=frame2 href='$link'>Read More</a></p>"; 
?> 
+0

당기는 문서의 빈도가 너무 많지 않은 경우 db 테이블에 저장하고 cron 또는 스케줄러로 자주 업데이트 할 수 있습니다 – Ibu

+0

Ibu - 감사합니다. 프론트 엔드 기술을 갖춘 디자이너. 그런 식으로 쓰는 것이 내 머리 위에있다. – mike

답변

1

확인 난 당신이 youu가 타임 아웃을 설정할 수 있습니다 컬

이 방법을 사용하는 것이 좋습니다 수 있습니다 페이지가 5 초 안에 응답하지 않으면 계속 진행할 수 있습니다.

$xml = simplexml_load_string($response);

+0

이것은 현재 작동하고있는 것 같습니다. Tumblr API가 문제를 일으킨 것 같습니다. 내 원래 코드가 다시 작동합니다. – mike

+0

아주 좋으므로 지금 cUrl은 file_get_contents와 비슷한 방법을 사용하는 simplexml_load_file()에 대한 더 나은 대안입니다. cUrl도 빠름 – Ibu

0
:

지금이처럼 간단한 XML을 사용할 수 있습니다 Documentation

  $url = "http://blog.yaytalent.com/api/read?type=post&start=1&num=2"; 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_HEADER, FALSE); 
      curl_setopt($ch, CURLOPT_NOBODY, FALSE); 
      curl_setopt($s,CURLOPT_TIMEOUT,5); // TIME OUT is 5 seconds 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
      $response = curl_exec($ch); 
      $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
      curl_close($ch); 

나는 학습에 당신과 goodluck는 대한 작품

UPDATE 희망

You can also do in this way:

function simplexml_load_file_from_url($url, $timeout = 20){ 

    $context = array('http' => array('timeout' => (int)$timeout)); 

    $data = file_get_contents($url, false, stream_context_create($context)); 

    if(!$data){ 
    trigger_error('Cannot load data from url: ' . $url, E_USER_NOTICE); 
    return false; 
    } 

    return simplexml_load_string($data); 
} 
0
curl_setopt(**$s**,CURLOPT_TIMEOUT,5); 

여기 Ibu가 생각하는대로 $ ch 여야합니다. 하지만 어쨌든 좋은 예입니다! 고맙습니다!

관련 문제