我的网站加载时间约为 45 秒。这是因为我从 tumblr 中提取了一些 XML,但我无法确定这是我的服务器的错误、tumblr 的错误还是其他因素。我可以让这个脚本在 5 秒后超时并回显“tumblr 已关闭”吗?而不是在近一分钟后超时?
我收到此错误:
警告:simplexml_load_file(http://blog.yaytalent.com/api/read?type=post&start=1&num=2)[function.simplexml-load-file]:无法打开流:/nfs/c08/中的连接超时h02/mnt/122191/domains/yaytalent.com/html/index.php 第 86 行
警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法在 / 中加载外部实体“http://blog.yaytalent.com/api/read?type=post&start=1&num=2” nfs/c08/h02/mnt/122191/domains/yaytalent.com/html/index.php 第 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
通过这种方式,您可以设置超时,如果页面在五秒内没有响应,您可以继续。文档 http://www.php.net/manual/en/book.curl.php
$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);
我希望这对你有用,祝你学习顺利
UPDATE:
现在你可以像这样使用简单的 xml:
$xml = simplexml_load_string($response);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)