在PHP中使用foreach获取JSON数据
站点包含JSON格式的月度数据。可以这样查询:在PHP中使用foreach获取JSON数据,php,json,foreach,Php,Json,Foreach,站点包含JSON格式的月度数据。可以这样查询: http://www.example.com?startdate=1-1-1985&enddate=31-1-1985 我希望能够运行一个脚本,从今天的月份开始获取我要查找的特定数据,然后反向工作,直到返回空数据。每个月的价值需要加起来。到目前为止,我得到的是: //Build base URL: $userid=$_GET['userid']; $startdate=/*Beginning of today's month*/; $en
http://www.example.com?startdate=1-1-1985&enddate=31-1-1985
我希望能够运行一个脚本,从今天的月份开始获取我要查找的特定数据,然后反向工作,直到返回空数据。每个月的价值需要加起来。到目前为止,我得到的是:
//Build base URL:
$userid=$_GET['userid'];
$startdate=/*Beginning of today's month*/;
$enddate=/*End of today's month*/;
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
//Set JSON variables:
$get=file_get_contents($url);
$json=json_decode($get);
//Set loop variables:
$value=0;
$month=0;
/*For each month from today backwards{
$number=$json->integer;
if($number!=null){
$value=$value+$number;
$month=$month+1;
}else{
break;
}
}
echo $value;
echo $month;
*/
我遇到问题的部分是第四部分的开始。如何运行一个循环,从今天月份的范围开始,获取$number,然后对上一个月重复,直到它到达返回null的月份?您可以使用
DateTime
对象及其关联方法(在本例中具体是sub
)通过每次减去1个月来向后计数。数组存储月份/日期,url使用变量计数器$i
最初,它的最大向后期限为20年(我猜这已经足够了),但很容易改变
$df='Y-m-d';
$userid=$_GET['userid'];
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('P1M');
$ts=date( $df, strtotime( date('Y-m-1') ) );
$tf=date( $df, strtotime( date('Y-m-1'). ' - 20 years') );
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
$dates=array();
$i=0;
while( $start->sub( $interval ) > $end ){
$dates[]=$start->format( $df );
if( $i > 1 ){
$startdate=$dates[ $i - 1 ];
$enddate=$dates[ $i ];
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
echo $url,'<br />';
/* uncomment when happy with mechanism */
/*
$data=file_get_contents( $url );
if( $data ) ) {
$json=json_decode( $data );
#process.....
break;
}
*/
}
$i++;
}
通过使用strotime()和mktime()函数,您可以在while中循环,直到得到空结果。下面的代码将在5个月内打印5个URL。当条件相应改变时
// define $i
$i=0;
$userid = '343';//$_GET['userid']; //dont forget to replace with userid
do{
$timestring = strtotime("now -$i month");
//get Month
$month = date('m',$timestring);
//get Year
$year = date('Y',$timestring);
// First date Month and Year
$startdate = date('d-m-Y', mktime(0, 0, 0, $month, 1, $year));
// Last date Month and Year
$enddate = date('d-m-Y', mktime(0, 0, 0, $month+1, 0,$year));
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
// commenting your code
//Set JSON variables:
//$get=file_get_contents($url);
//$json=json_decode($get);
// $number=$json->integer;
echo $url;
echo "\n";
$i++;
}while ($i!=5); // change while condition according to your requirement. while ($number!=null)
输出:
http://www.example.com?userid=343&startdate=01-12-2017&enddate=31-12-2017
http://www.example.com?userid=343&startdate=01-11-2017&enddate=30-11-2017
http://www.example.com?userid=343&startdate=01-10-2017&enddate=31-10-2017
http://www.example.com?userid=343&startdate=01-09-2017&enddate=30-09-2017
http://www.example.com?userid=343&startdate=01-08-2017&enddate=31-08-2017
mktime()。另外,如果您喜欢处理更具“可读性”的日期更改,也可以strotime(“现在-每月$m”)
。
http://www.example.com?userid=343&startdate=01-12-2017&enddate=31-12-2017
http://www.example.com?userid=343&startdate=01-11-2017&enddate=30-11-2017
http://www.example.com?userid=343&startdate=01-10-2017&enddate=31-10-2017
http://www.example.com?userid=343&startdate=01-09-2017&enddate=30-09-2017
http://www.example.com?userid=343&startdate=01-08-2017&enddate=31-08-2017