doupeng8419 2015-08-03 14:44
浏览 245

Foreach重复多次

Good afternoon. I want to scrobble tracks from the radio, but I have a problem with my code which runs with 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 . "
", 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;
    }
  }
}    



?>

One line from file, but foreach works many times (5-6 or 15-16). Why? I want to make foreach work only 1 time. Please help me. What's the problem?

update

contents of 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序