php - Using simplexml_load_file to pull from tumblr - timing out every time -
my site taking ~45 seconds load. it's because i'm pulling in xml tumblr, can't figure out if server's fault, tumblr's fault, or other factor. can script time out in 5 seconds , echo 'tumblr down'; instead of timing out after minute?
i'm getting error:
warning: simplexml_load_file(http://blog.yaytalent.com/api/read?type=post&start=1&num=2) [function.simplexml-load-file]: failed 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 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
with code:
<?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>"; ?>
ok in case can suggest use curl
this way youu can set timeout , if page doesnt respond in 5 seconds can move on. 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 5 seconds curl_setopt($ch, curlopt_returntransfer, true); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, curlinfo_http_code); curl_close($ch);
i hope works , goodluck in learning
update:
now can use simple xml this:
$xml = simplexml_load_string($response);
Comments
Post a Comment