gpt4 book ai didi

php - 遍历数组 PHP

转载 作者:数据小太阳 更新时间:2023-10-29 02:42:51 26 4
gpt4 key购买 nike

我正在尝试循环访问 cURL 数组响应以生成 XML 文件。但是,我生成的文件似乎都具有完全相同的内容——我不知道这是否是正确的方法,或者我是否应该使用数组,但我似乎无法弄清楚,可以使用一些新的眼睛.基本上,我希望每个 $playlist 的内容都在其自己的单独文件中。

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);
}

感谢任何能提供帮助的人!

最佳答案

我会使用 SimpleXML或内置于 PHP 中的 DOM 功能来生成 XML - 它非常强大,您只需传入属性名称、值和节点。

关于为什么您每次都得到相同的结果,您每次都发出相同的 CURL 请求,所以您希望得到相同的结果。

关于php - 遍历数组 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3936007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com