I am trying to execute a cURL array response loop to generate XML files. However, my generated files have all the same contents - I don’t know if this is correct, or if I have to use an array, but I can’t understand it and use fresh eyes. In principle, I want each content to $playlistbe in a separate file.
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 "Successfully created manifest $i<br />";
}
function generateXML($i) {
$xml_code = array($i);
$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";
$options = array(
'page_size' => '75',
'playlist_ids' => "3690598001,3684920001,8193433701",
'video_fields' => 'referenceId,creationDate'
);
$post_str = '';
foreach($options as $key=>$value) {
$post_str .= $key.'='.urlencode($value).'&';
}
$post_str = substr($post_str, 0, -1);
$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);
$result = json_decode($obj);
$playlists = $result->{'items'};
foreach ($playlists as $playlist){
foreach ($playlist->{'videos'} as $video){
$creation_date_ms = $video->creationDate;
$creation_date_s = $creation_date_ms / 1000;
$date = date('Ymd',$creation_date_s);
$time = strtotime($date);
$ref_id = $video->referenceId;
switch ($ref_id) {
case "":
break;
default:
if ($time <= strtotime('20100519')) {
$xml_code[$i] .= reencode_from_existing_source($ref_id);
}
break;
}
}
}
$xml_code[$i] .= '</publisher-upload-manifest>';
return($xml_code[$i]);
}
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);
}
Thanks to everyone who can help!
source
share