php获取数据问题

天台的故事 2017-05-25 01:10:34

<?php

$i = 1;


for ($i=1; $i<5; $i++){

echo "第".$i."页数据"."<br><br>";
$test = file_get_contents('http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=FD&sty=MTE&mkt=1&code=600000&st='.$i.'&sr='.$i.'&p='.$i.'&ps=25');
echo $test."<br><br><br>";
}

?>

上面是我一段代码,主要是获取file_get_contents参数链接返回的json数据,循环5次是代表链接数据是一个分页数据,i=1,代表获取第一页,以此类推,但不知道为什么有时候获取得到数据有时候又获取不到数据。如图:

...全文
1632 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
谢高升 2017-09-22
  • 打赏
  • 举报
回复
file_get_content 效率低改用 curl
果酱很好吃 2017-05-25
  • 打赏
  • 举报
回复
这个链接是你的?若不是,你怎么知道是什么原因,有可能是别人网站做了限制呢?比如访问频率 只要能解决不能获取的情况就行了
天台的故事 2017-05-25
  • 打赏
  • 举报
回复
引用 1 楼 jam00 的回复:
没获取到就重试一遍

$i = 1;
for ($i=1; $i<5; $i++){
 
    echo "第".$i."页数据"."<br><br>";
	$url = 'http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=FD&sty=MTE&mkt=1&code=600000&st='.$i.'&sr='.$i.'&p='.$i.'&ps=25';
    $test = file_get_contents($url);
	if($test==''){
		$test = file_get_contents($url);
	}
    echo $url.'<br>'.$test."<br><br><br>";
}
这个我也想过,但是boss说不行,要找出原因
果酱很好吃 2017-05-25
  • 打赏
  • 举报
回复
没获取到就重试一遍

$i = 1;
for ($i=1; $i<5; $i++){
 
    echo "第".$i."页数据"."<br><br>";
	$url = 'http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=FD&sty=MTE&mkt=1&code=600000&st='.$i.'&sr='.$i.'&p='.$i.'&ps=25';
    $test = file_get_contents($url);
	if($test==''){
		$test = file_get_contents($url);
	}
    echo $url.'<br>'.$test."<br><br><br>";
}

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧