更改PHP脚本的输出以使用POST方法

时间:2014-10-13 17:01:17

标签: php xml parsing post

我缺乏经验,但有人能指出我正确的方向,我可以更改下面的PHP脚本,输出从XML文件(标题,链接,描述等)解析的每个变量作为POST方法而不仅仅是HTML页面?

<?php
$html = "";
$url = "http://api.brightcove.com/services/library?command=search_videos&any=tag:SMGV&output=mrss&media_delivery=http&sort_by=CREATION_DATE:DESC&token= // this is where the API token goes";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces

for($i = 0; $i < 80; $i++){
  $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;
  $html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p>
    <iframe width='480' height='270' src='http://link.brightcove.com/services/player/bcpid3742068445001?bckey=AQ~~,AAAABvaL8JE~,ufBHq_I6FnyLyOQ_A4z2-khuauywyA6P&bctid=$titleid&autoStart=false' frameborder='0'></iframe><hr/>";/* this embed code is from the youtube iframe embed code format but is actually using the embedded Ooyala player embedded on the Campus Insiders page. I replaced any specific guid (aka video ID) numbers with the "$guid" variable while keeping the Campus Insider Ooyala publisher ID, "eb3......fad" */
}
echo $html;
?>

@ V.Radev这是另一个使用cURL的PHP​​脚本,我认为它将与API一起使用,我试图将数据发送到:

<?PHP
  $url = 'http://api.brightcove.com/services/post';

  //open connection
  $ch = curl_init($url);

  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_POST, 1);
  curl_setopt($ch,CURLOPT_POSTFIELDS, '$title,$descripton,$url' . stripslashes($_POST['$title,$description,$url']));
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

  // Enable for Charles debugging
  //curl_setopt($ch,CURLOPT_PROXY, '127.0.0.1:8888'); 

  $result = curl_exec($ch);
  curl_close($ch);

  print $result;
?>

我的问题是,如何将我的Feed解析脚本(标题,描述,URL)中的变量传递给这个新脚本?

我有来自Brightcove的代码,我可以从我的解析器脚本输出变量并发送到这个PHP脚本,以便数据转到API吗?

<?php

  // This code example uses the PHP Media API wrapper
  // For the PHP Media API wrapper, visit http://docs.brightcove.com/en/video-cloud/open-source/index.html

  // Include the BCMAPI Wrapper
  require('bc-mapi.php');

  // Instantiate the class, passing it our Brightcove API tokens (read, then write)
  $bc = new BCMAPI(
    '[[READ_TOKEN]]',
    '[[WRITE_TOKEN]]'
  );

  // Create an array of meta data from our form fields
  $metaData = array(
    'name' => $_POST['bcVideoName'],
    'shortDescription' => $_POST['bcShortDescription']
  );

  // Move the file out of 'tmp', or rename
  rename($_FILES['videoFile']['tmp_name'], '/tmp/' . $_FILES['videoFile']['name']);
  $file = '/tmp/' . $_FILES['videoFile']['name'];

  // Create a try/catch
  try {
    // Upload the video and save the video ID
    $id = $bc->createMedia('video', $file, $metaData);
          echo 'New video id: ';
          echo $id;
  } catch(Exception $error) {
    // Handle our error
    echo $error;
    die();
  }
?>

1 个答案:

答案 0 :(得分:0)

Post是访问特定页面或资源的请求方法。使用echo,您发送的数据意味着您正在响应。在此页面中,您只能添加响应标头并使用请求方法(如post,get,put等)访问它。

编辑API请求,如评论中所述:

$curl = curl_init('your api url');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $your_data_to_send);
$result_from_api = curl_exec($curl); 
curl_close($curl);