Foreach重复多次

时间:2015-08-03 14:44:24

标签: php arrays foreach

下午好。我想从收音机中搜索出来的曲目,但是我的代码与Cron一起出现了问题:

<?php

header( 'Content-type: text/html; charset=UTF-8' );
function compareFiles( $file_a, $file_b ) {
  if ( filesize( $file_a ) == filesize( $file_b ) ) {
    $fp_a = fopen( $file_a, 'rb' );
    $fp_b = fopen( $file_b, 'rb' );

    while ( ( $b = fread( $fp_a, 4096 ) ) !== false ) {
      $b_b = fread( $fp_b, 4096 );
      if ( $b !== $b_b ) {
        fclose( $fp_a );
        fclose( $fp_b );

        return false;
      }
    }

    fclose( $fp_a );
    fclose( $fp_b );

    return true;
  }

  return false;
}

if ( compareFiles( "/var/log/icecast2/playlist.log", "/home/playlist.log" ) == false ) {
  copy( "/var/log/icecast2/playlist.log", "/home/playlist.log" );
  $file = file( "/home/playlist.log" );
  $file_array = $file;
  $far = array_slice( $file, -2 );
  $far = array_reverse( $far );
  $dsz = print_r( $far );
  file_put_contents( $dsz, '/home/arr.txt' );
  foreach ( $far as $arr1 => $arr2 ) {
    $elem = explode( '|', $arr2 );
    $is_low = strpos( $arr2, 'hcore_48' );
    if ( $is_low != true ) {
      //  $c = date_create_from_format('', $elem[0]);
      //  $time= $c->format('H:m');
      $song = $elem[ 3 ];
      $infos_artist = explode( ' - ', $song );
      $infos_artist[ 0 ] = trim( $infos_artist[ 0 ] );
      $infos_artist[ 1 ] = trim( $infos_artist[ 1 ] );
      if ( $infos_artist[ 0 ] != "HCore FM" ) {
        $partnerId = "xxx";

        $partnerKey = "xxx";

        $stationId = "xxx";

        $artist_tunein = str_replace( ' ', '+', $infos_artist[ 0 ] );

        $title_tunein = str_replace( ' ', '+', $infos_artist[ 1 ] );

        $commercial = false;


        if ( ( $infos_artist[ 0 ] == "HCore FM" ) || ( $infos_artist[ 0 ] == "Promo" ) ) $commercial = true;


        $url_tunein = "http://air.radiotime.com/Playing.ashx?partnerId=" . $partnerId . "&partnerKey=" . $partnerKey . "&id=" . $stationId . "&title=" . $title_tunein . "&artist=" . $artist_tunein . "&commercial=" . $commercial;


        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_URL, $url_tunein );
        curl_setopt( $ch, CURLOPT_HEADER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, "&title=" . $title_tunein . "&artist=" . $artist_tunein . "&commercial=" . $commercial );
        $data = curl_exec( $ch );

        $server_output = simplexml_load_string( $data );
        curl_close( $ch );


        $log = "/home/tuneinlog.txt";
        $url = "https://api.vk.com/method/audio.search?q=" . $artist_tunein . "+-+" . $title_tunein . "&count=1&access_token=932fe2e4fc4e89179bdeb223f382394a43684125d7ebdb3382878866d190cef8f2bb6352d0efe82d64057";
        $result = file_get_contents( $url );
        // Will dump a beauty json :3
        $obj = json_decode( $result, true );
        $vk = "/home/vklog.txt";
        if ( $result == "{\"response\":[0]}" ) {
          file_put_contents( $vk, PHP_EOL . $artist_tunein . " - " . $title_tunein . "\r\n", FILE_APPEND );
        }
        $aid = $obj[ 'response' ][ '1' ][ 'aid' ];
        $oid = $obj[ 'response' ][ '1' ][ 'owner_id' ];
        $urlnh = "https://api.vk.com/method/audio.setBroadcast?audio=" . $oid . "_" . $aid . "&target_ids=-91685382&access_token=xxx";
        $res = file_get_contents( $urlnh );
      }
      break;
    }
  }
}    



?>

来自档案的一行,但是foreach多次工作(5-6或15-16)。为什么?我想只做一次foreach工作。请帮我。问题是什么?

更新

var_dump($far)

的内容
Array ( 
    [0] => 03/Aug/2015:18:00:46 +0300|/hcore_128|1|The Union Underground - Call Me (Blondie cover) 
    [1] => 03/Aug/2015:18:00:47 +0300|/hcore_48|0|The Union Underground - Call Me (Blondie cover) 
)

0 个答案:

没有答案