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

将PHP脚本的输出更改为使用POST方法,php,xml,parsing,post,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=CREA

请容忍我在这里的经验不足,但是有谁能告诉我如何更改下面的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;
?>
我的问题是,如何将提要解析脚本标题、描述、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();
  }
?>

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

请研究如何使用PHP脚本发出POST请求,这个主题应该已经足够涵盖了。感谢对此的澄清,我假设我需要在这个脚本中调用类似于cURL的东西,以便通过PUT或POST传递数据。cURL也是发出请求的一种方法。你正在作出回应。您可以通过post方法对此文件发出请求,但不能使用post方法输出它。你能详细介绍一下你想要达到的目标吗?我意识到我必须改变剧本,只是不知道怎么做。我想做的是从提要中获取这个脚本正在解析的所有元素,并通过它们的API将这些元素(视频标题、描述、URL)发送或发布到视频云托管服务。我只是经验不足,不知道如何实施这最后一步。我有API文档,我知道我需要使用POST方法和create_video命令,但我不知道如何设置。对于这个API请求,cURL确实是首选方法。请检查以上对我的答案的更改,并注意,如果API要求,您可能需要对发送数据进行json编码。我在cURL选项中添加了returntransfer,以防您期待API的响应。再次感谢您的指导,我非常感谢。请看我上面的更新帖子,我尝试创建了一个PHP脚本,其中包含了您的建议,我认为它将与我试图发送此数据的API一起工作。我的问题是:您建议我如何将解析脚本中的数据发送到这个使用cURL的新脚本?