コ-ド全体は以下となります。
後出しで申し訳ありません。。
※表示がおかしくなるので一部大文字で表記しております。
<?php
require( dirname( __FILE__ ) . '/config.php' );
file_put_contents( $lock_file, getmypid() );
$gc = $argv[1];
$out_file = dirname( __FILE__ ) . '/csv/yolp-' . $gc . '.csv';
$tmp_file = dirname( __FILE__ ) . '/tmp/yolp-' . $gc . '.csv';
$csv_headers = array(
'post_title',
'station',
);
file_put_contents( $tmp_file, array2csv( $csv_headers ), FILE_APPEND );
$batch_size = 100;
$int_ac = 1;
while ( $int_ac < 48 )
{
$target_ac = str_pad( $int_ac, 2, '0', STR_PAD_LEFT );
$xml = ppn_request( $appid, $cid, 1, 1, $gc, $target_ac );
$total = (int)$xml->ResultInfo->Total;
$start = 1;
$retrieved = 0;
while ( $retrieved < $total )
{
$xml = local_search_request( $appid, $cid, $start, $batch_size, $gc, $target_ac );
foreach ( $xml->Feature as $f )
{
$ac = $f->Property->GovernmentCode;
$latlon = explode( ',', $f->Geometry->Coordinates );
$station = '';
foreach($f->Property->Station as $s)
{
$station = $station . $s->Railway . ' ' . $s->Name . "\n";
}
$csv_line = array(
$f->Name,
$station,
);
file_put_contents( $tmp_file, array2csv( $csv_line ), FILE_APPEND );
}
$retrieved += (int)$xml->ResultInfo->Count;
$start += (int)$xml->ResultInfo->Count;
}
$int_ac += 1;
}
rename( $tmp_file, $out_file );
unlink( $lock_file );
function ppn_request( $appid, $cid, $start = 1, $num = 10, $gc = null, $ac = null )
{
$api = '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~';
$url = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
if ( $gc !== null )
{
$url = $url . "&gc={$gc}";
}
if ( $ac !== null )
{
$url = $url . "&ac={$ac}";
}
$res = file_get_contents($url);
$xml = simplexml_load_string($res);
return $xml;
}
function array2csv( $array )
{
$fp = fopen('php://memory', 'r+');
fputcsv($fp, $array);
rewind($fp);
$csv_line = stream_get_contents($fp);
fclose($fp);
return $csv_line;
}