2010-10-14 79 views
3

我試圖通過一個cURL數組響應來生成XML文件。然而,我生成的文件似乎都有完全相同的內容 - 我不知道這是正確的方法,還是我應該使用數組,但我似乎無法弄清楚,可以使用一些新鮮的眼睛。基本上,我希望每個$playlist的內容都在它自己的單獨文件中。在數組中循環PHP

for ($i=0; $i<=14; $i++) { 
    $xml_data = generateXML($i); 
    $fileName = "bc_manifest_$i.xml"; 
    $fileHandle = fopen($fileName, 'wb') or die("can't open file"); 
    fwrite ($fileHandle, $xml_data); 
    fclose($fileHandle); 
    //echo $xml_data; 
    echo "Successfully created manifest $i<br />"; 
    } 

// The holy grail 
function generateXML($i) { 
    $xml_code = array($i); 
    // Start the beginning of the xml doc and save it to our reoccuring xml_code var 
    $xml_code[$i] .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; 
    $xml_code[$i] .= '<publisher-upload-manifest publisher-id="xxxxxxx" preparer="Dave" report-success="TRUE">' . "\n"; 

    // Set options to send to brightcove 
    // @page_size 
    $options = array(
       'page_size' => '75', 
       'playlist_ids' => "3690598001,3684920001,8193433701", 
       'video_fields'  => 'referenceId,creationDate'  
    ); 

    // URL Encode the options to prepare for cURL send 
    $post_str = ''; 
    foreach($options as $key=>$value) { 
     $post_str .= $key.'='.urlencode($value).'&'; 
    } 
    $post_str = substr($post_str, 0, -1); 

    // Initiate cURL and send request 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://api.brightcove.com/services/library?command=find_playlists_by_ids&token=xxxxxxxxxx&'); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $obj = curl_exec($ch); 
    curl_close($ch); 

    // Decode json response 
    $result = json_decode($obj); 

    // Get playlists from the items object 
    $playlists = $result->{'items'}; 

    //var_dump($playlists); 

    // For each playlist... 
    foreach ($playlists as $playlist){ 
     foreach ($playlist->{'videos'} as $video){ 
      // Convert the creation date to something not so robotic.. 
      $creation_date_ms = $video->creationDate; 
      $creation_date_s = $creation_date_ms/1000; 
      $date = date('Ymd',$creation_date_s); 
      $time = strtotime($date); 
      //echo $time."<br />"; 

      // and set the reference id for each video. 
      $ref_id = $video->referenceId; 

      // Phew! Now, let's first check that there is a reference id for the video. If not, no biggie. 
      switch ($ref_id) { 
       case "": 
        break; 
       default: 
        // If so, run the function to create an xml object for that video! 
        if ($time <= strtotime('20100519')) { 
         $xml_code[$i] .= reencode_from_existing_source($ref_id); 
        } 
        break; 
      }   
     } 
    } 
    // Finish him. 
    $xml_code[$i] .= '</publisher-upload-manifest>'; 
    return($xml_code[$i]); 
} //endxml 


// Creates an xml object for the passed reference id. 
function reencode_from_existing_source($ref_id){ 
    $xml_obj = '<reencode-from-existing-source 
    title-refid="' . $ref_id . '" 
    encode-to="MP4" 
    encode-multiple="TRUE" 
    overwrite-images="FALSE" />' . "\n"; 
    return ($xml_obj); 
} 

感謝任何能幫助到的人!

+1

你總是用cURL做同樣的請求。 – Narf 2010-10-14 19:01:42

+1

如果源代碼被降低到最低程度仍然存在問題,則問題對社區更有用。 – nalply 2012-08-06 14:51:03

回答

0

這爲我在過去的工作:http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/

它的相當不錯,在這個URL記錄,但讓我知道如果你有問題。

$xml = ArrayToXML::toXML($data_array, 'name_of_root_node'); 

這應該讓你開始。對於第二個參數,請將其更改爲適合您的任何根節點名稱。

編輯:添加從上面的鏈接ArrayToXML類的片段,以減輕鏈接腐爛的影響。

class ArrayToXML 
{ 
    /** 
    * The main function for converting to an XML document. 
    * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. 
    * 
    * @param array $data 
    * @param string $rootNodeName - what you want the root node to be - defaultsto data. 
    * @param SimpleXMLElement $xml - should only be used recursively 
    * @return string XML 
    */ 
    public static function toXml($data, $rootNodeName = 'data', $xml=null) 
    { 
     // turn off compatibility mode as simple xml throws a wobbly if you don't. 
     if (ini_get('zend.ze1_compatibility_mode') == 1) 
     { 
      ini_set ('zend.ze1_compatibility_mode', 0); 
     } 

     if ($xml == null) 
     { 
      $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />"); 
     } 

     // loop through the data passed in. 
     foreach($data as $key => $value) 
     { 
      // no numeric keys in our xml please! 
      if (is_numeric($key)) 
      { 
       // make string key... 
       $key = "unknownNode_". (string) $key; 
      } 

      // replace anything not alpha numeric 
      $key = preg_replace('/[^a-z]/i', '', $key); 

      // if there is another array found recrusively call this function 
      if (is_array($value)) 
      { 
       $node = $xml->addChild($key); 
       // recrusive call. 
       ArrayToXML::toXml($value, $rootNodeName, $node); 
      } 
      else 
      { 
       // add single node. 
           $value = htmlentities($value); 
       $xml->addChild($key,$value); 
      } 

     } 
     // pass back as string. or simple xml object if you want! 
     return $xml->asXML(); 
    } 
} 
+0

請勿鏈接到您在其他地方找到的源代碼。 – nalply 2012-08-06 14:51:51

+0

docs here(stackoverflow)表明只有一個鏈接不好,沒有上下文。雖然我承認我的答案可能不是最好的,但我不覺得它違反了任何規則或慣例(沒有人說你不能鏈接到外部資源 - 而且這經常發生)。謹慎澄清? – gregghz 2012-08-06 15:07:59

+0

問題是鏈接腐爛。你永遠不知道這個鏈接是否還在一年左右的時間內運行。 – nalply 2012-08-06 15:09:54

1

我會使用PHP內置的SimpleXML或DOM功能來生成XML - 這是非常強大的,你只需要在屬性名稱,值和節點通過。

關於你爲什麼每次都得到相同的結果,你每次都做同樣的CURL請求,所以你會期望得到相同的結果。