本文介绍了使用 simplexml_load_file 从 tumblr 中提取 - 每次都超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站加载时间约为 45 秒.这是因为我从 tumblr 中提取了一些 XML,但我无法确定这是我的服务器的错误、tumblr 的错误还是其他一些因素.我可以让这个脚本在 5 秒内超时并回显 'tumblr is down' 吗?而不是在将近一分钟后超时?

My site is taking ~45 seconds to load. It's because I'm pulling in some XML from tumblr, but I can't figure out if this is my server's fault, tumblr's fault, or some other factor. Can I get this script to time out in 5 seconds and echo 'tumblr is down'; instead of just timing out after nearly a minute?

我收到此错误:

警告:simplexml_load_file(http://blog.yaytalent.com/api/read?type=post&start=1&num=2) [function.simplexml-load-file]:无法打开流:连接超时在第 86 行的/nfs/c08/h02/mnt/122191/domains/yaytalent.com/html/index.php 中

警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法加载外部实体http://blog.yaytalent.com/api/read?type=post&start=1&num=2" in/nfs/c08/h02/mnt/122191/domains/yaytalent.com/html/index.php 第 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>";
?>

推荐答案

好的,我可以建议你使用 cUrl

ok in this case i can suggest you use cUrl

通过这种方式您可以设置超时,如果页面在五秒内没有响应,您可以继续.文档

This way youu can set a timeout and if the page doesnt respond in five seconds you 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 is 5 seconds
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
            $response = curl_exec($ch); 
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
            curl_close($ch); 

希望对你有用,祝你学习愉快

I hope that works for you and goodluck in your learning

更新:

现在您可以像这样使用简单的 xml:

now you can use your simple xml like this:

$xml = simplexml_load_string($response);

这篇关于使用 simplexml_load_file 从 tumblr 中提取 - 每次都超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:23