【问题标题】:How to run a search for any movie using the Rotten Tomatoes API?如何使用烂番茄 API 搜索任何电影?
【发布时间】:2014-09-25 05:14:33
【问题描述】:

在连接到Rotten Tomatoes API 时,谁能帮助我使用 PHP 创建电影搜索?

在烂番茄网站上,他们为您提供了如何获取特定电影内容的示例代码,如下所示:

<?php
$apikey = 'insert_your_api_key_here';
$q = urlencode('Toy Story'); // make sure to url encode an query parameters

// construct the query with our apikey and the query we want to make
$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=' . $apikey . '&q=' . $q;

// setup curl to make a call to the endpoint
$session = curl_init($endpoint);

// indicates that we want the response back
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// exec curl and get the data back
$data = curl_exec($session);

// remember to close the curl session once we are finished retrieveing the data
curl_close($session);

// decode the json data to make it easier to parse the php
$search_results = json_decode($data);
if ($search_results === NULL) die('Error parsing json');

// play with the data!
$movies = $search_results->movies;
echo '<ul>';
foreach ($movies as $movie) {
  echo '<li><a href="' . $movie->links->alternate . '">' . $movie->title . " (" . $movie->year . ")</a></li>";
}
echo '</ul>';


?>

我是 PHP 的初学者,所以一个例子会很棒。我已经设法用 JavaScript 解决了这个问题,但是由于更新,我必须托管页面的服务器不会显示它。所以现在我将不得不转向 PHP,因为它也会显示上面的代码。

【问题讨论】:

  • 您能准确概述您遇到的问题吗?您是否尝试过运行此示例以查看它是否有效?添加您的密钥并尝试一下,开始。请记住,在没有相应尝试的情况下询问示例往往会获得反对/近距离投票,因此通常最好先试一试,然后提供您坚持的实际代码。
  • 是的,当然我已经用钥匙试过了,它当然可以工作。但是如果我想要一个搜索栏来搜索任何类型的电影,我不知道该怎么做
  • 您需要&lt;form&gt; 来捕获数据,将method 设置为get 并将action 设置为与您的脚本相同的路径名(例如search-movies.php),然后使用input type="text" 控件获取电影名称,使用input type="submit" 提交搜索。从那里,您可以提取文本并将其提供给上述脚本。

标签: php rotten-tomatoes


【解决方案1】:

您需要创建一个带有文本字段的表单。将字段命名为 q,以便它与您的示例代码一起使用。 将表单发布到示例 php 脚本的 url。 删除这一行:

$q = urlencode('Toy Story');

用这个替换它:

$q = urlencode($_POST['q']);

这应该让你开始。

请注意,不会对 $_GET 进行清理。我只是根据您发布的示例代码提供运行搜索表单所需的基础知识。

如果您不知道如何创建表单,这里有一个教程链接:

http://www.tizag.com/phpT/forms.php

【讨论】:

    猜你喜欢
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多