【问题标题】:Change Output of PHP Script to use POST Method更改 PHP 脚本的输出以使用 POST 方法
【发布时间】:2014-10-13 17:01:17
【问题描述】:

请忍受我在这里的缺乏经验,但任何人都可以指出正确的方向,让我了解如何更改下面的 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();
  }
?>

【问题讨论】:

  • 请研究如何使用 PHP 脚本发出 POST 请求,该主题应该已经充分涵盖了。

标签: php xml parsing post


【解决方案1】:

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

如 cmets 中所述编辑 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);

【讨论】:

  • 感谢您对此的澄清,我假设我需要在此脚本中调用类似 cURL 的内容才能通过 PUT 或 POST 传递数据?
  • cURL 也是一种发出请求的方式。你正在做出回应。您可以通过 post 方法向该文件发出请求,但您不能使用 post 方法将其输出。您能否详细说明您想要实现的目标?
  • 我意识到我必须更改脚本,只是不知道该怎么做。我想要做的是获取该脚本从提要中解析的所有元素,并通过其 API 将这些元素(视频标题、描述、URL)发送或发布到视频云托管服务。我只是没有足够的经验知道如何实施这最后一步。我有 API 文档,我知道我需要使用 POST 方法和 create_video 命令,但我不知道如何设置。
  • 对于这个 API 请求,cURL 确实是首选方法。请检查我上面的答案的更改,并注意如果 API 需要,您可能需要对您的发送数据进行 json 编码。我在 cURL 选项中添加了一个 returntransfer 以防您期待 API 的响应。
  • 再次感谢您的指导,我真的很感激。请参阅上面我更新的帖子,我尝试创建一个包含您的建议的 PHP 脚本,并且我认为它可以与我尝试将这些数据发送到的 API 一起使用。我的问题是:你建议我如何将数据从解析脚本发送到这个使用 cURL 的新脚本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2016-03-08
  • 2016-02-25
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多