通过会话将PHP Parser脚本变量输出到PHP表单


Output PHP Parser Script Variables to PHP form via Sessions

PHP新手,请耐心等待…

我正在尝试将这个simpleXml解析器脚本的输出变量发送到另一个PHP文件,该文件应该将数据发送到Brightcove的Media API。

发送脚本:

<?php
session_name("FeedParse");
session_start();
$_SESSION['bcName'] = $title;
$_SESSION['shortDescription'] = $description;
$_SESSION['remoteUrl'] = $videoFile;
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos 
  $title = $xml->channel->item[$i]->video;
  $link = $xml->channel->item[$i]->link;
  $title = $xml->channel->item[$i]->title;
  $pubDate = $xml->channel->item[$i]->pubDate;
  $description = $xml->channel->item[$i]->description;
  $titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
  $m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
  $videoFile = $m_attrs["url"];
  $html .= //"<h3>$title</h3>$description<p>$pubDate<p>$url<p>Video ID: $titleid<p>
print $title;
print $description;
print $videoFile;
// echo $html;/* tutorial for this script is here https://www.youtube.com/watch?v=4ZLZkdiKGE0 */
}
//http://support.brightcove.com/en/video-cloud/docs/media-write-api-php-example-upload-video
?>

接收脚本:

  <?php 
session_start();
$title = $_SESSION['bcName'];
$description = $_SESSION['shortDescription'];
$videoFile = $_SESSION['remoteUrl'];
// Instantiate the Brightcove class
$bc = new Brightcove(
    '//readtoken//', //Read Token BC 
    '//writetoken//' //Write Token BC 
);
// Set the data for the new video DTO using the form values
$metaData = array(
  '$title' => $_POST['bcName'],
  '$description' => $_POST['bcShortDescription'],
);
//changed all the code below to what i think works for remoteUrl and URLs as opposed to actual video files
// Rename the file to its original file name (instead of temp names like "a445ertd3")
$url = $_URL['remoteUrl'];
//rename($url['tmp_name'], '/tmp/' . $url['name']);
//$url = '/tmp/' . $url['name'];
// Send the file to Brightcove
//Actually, this has been changed to send URL to BC, not file
echo $bc->createVideo($url,$metaData);
class Brightcove {
        public $token_read = 'UmILcDyAFKzjtWO90HNzc67X-wLZK_OUEZliwd9b3lZPWosBPgm1AQ..'; //Read Token from USA Today Sports BC
        public $token_write = 'svP0oJ8lx3zVkIrMROb6gEkMW6wlX_CK1MoJxTbIajxdn_ElL8MZVg..'; //Write Token from USA Today Sports BC 
        public $read_url = 'http://api.brightcove.com/services/library?';
        public $write_url = 'http://api.brightcove.com/services/post';
  public function __construct($token_read, $token_write = NULL ) {
    $this->token_read = $token_read;
    $this->token_write = $token_write;
  }
  public function createVideo($url = NULL, $meta) {
    $request = array();
    $post = array();
    $params = array();
    $video = array();
    foreach($meta as $key => $value) {
      $video[$key] = $value;
    }
    $params['token'] = $this->token_write;
    $params['video'] = $video;
    $post['method'] = 'create_video';
    $post['params'] = $params;
    $request['json'] = json_encode($post);
    if($file) {
      $request['file'] = '@' . $file;
    }
    // Utilize CURL library to handle HTTP request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $this->write_url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_VERBOSE, TRUE );
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($curl, CURLOPT_TIMEOUT, 300);
    $response = curl_exec($curl);
    curl_close($curl);
    // Responses are transfered in JSON, decode into PHP object
    $json = json_decode($response);
    // Check request error code and re-call createVideo if request
                // returned a 213 error. A 213 error occurs when you have 
                // exceeded your allowed number of concurrent write requests
    if(isset($json->error))  {
      if($json->error->code == 213) {
        return $this->createVideo($url, $meta);
      } else {
        return FALSE;
      }
    } else {
      return $response;
    }
  }
}
?>​

我是否在这里设置了正确工作的会话?关于接收PHP脚本为什么不接收PHP提要解析器脚本输出的数据/变量,有什么想法吗?

在发送脚本中,您似乎在脚本的开头设置了会话变量,并且希望它们在本地变量更改时得到更新。按照你的写作方式,这是不会发生的。

您可以通过引用分配变量,在本地变量名之前加上一个与号(&)来实现这一点,但在某些情况下,这可能会变得有点棘手,最好跳过这个难题,直接更新会话变量。

然而,另一个问题是,您试图将多个值(50,如代码注释所示)存储到标量会话变量中。因此,每次循环迭代时,它都会覆盖以前的值。也许更好的方法是使用阵列结构:

<?php
session_name("FeedParse");
session_start();
$_SESSION['videos'] = array(); // initialize a session variable 'videos' to be an array
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos 
  $m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
  // on each loop iteration, create a new array structure with the video info in it and
  // push it onto the 'video' array session variable
  $video = array(
    'bcName' => $xml->channel->item[$i]->video,
    'shortDescription' => $xml->channel->item[$i]->description,
    'remoteUrl' => $m_attrs["url"],
  );
  $_SESSION['videos'][] = $video;
}

然后,在接收脚本上,您将循环通过$_SESSION['videos']:

<?php 
session_start();
// Instantiate the Brightcove class
$bc = new Brightcove(
    'UmILcDyAFKzjtWO90HNzc67X-wLZK_OUEZliwd9b3lZPWosBPgm1AQ..', //Read Token from USA Today Sports BC 
    'svP0oJ8lx3zVkIrMROb6gEkMW6wlX_CK1MoJxTbIajxdn_ElL8MZVg..' //Write Token from USA Today Sports BC 
foreach ((array)$_SESSION['videos'] as $video) {
    $title = $video['bcName'];
    $description = $video['shortDescription'];
    $videoFile = $video['remoteUrl'];
    // The code below this line may need to be adjusted. It does not seem quite right.
    // Are you actually sending anything to this script via POST? Or should those just
    // be the values we set above?
    // What is $_URL? Should that just be the $videoFile value?

    // Set the data for the new video DTO using the form values
    $metaData = array(
      '$title' => $_POST['bcName'],
      '$description' => $_POST['bcShortDescription'],
    );
    //changed all the code below to what i think works for remoteUrl and URLs as opposed to actual video files
    // Rename the file to its original file name (instead of temp names like "a445ertd3")
    $url = $_URL['remoteUrl'];
    //rename($url['tmp_name'], '/tmp/' . $url['name']);
    //$url = '/tmp/' . $url['name'];
    // Send the file to Brightcove
    //Actually, this has been changed to send URL to BC, not file
    echo $bc->createVideo($url,$metaData);
}

重要提示:

请记住,这将为会话中的每个视频调用API一次(听起来每次最多50个)。因此,您将在每次运行此脚本时发出50个cURL请求。这看起来有点沉重,但也许这是意料之中的事。如果他们的API允许您将数据编译成一个调用并一次发送所有数据,而不是连接、发送数据、解析响应和断开连接50次,那么值得研究一下。