在连接到烂番茄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,因为它也显示上面的代码。
您需要创建一个带有文本字段的表单。将字段命名为q,以便它能与示例代码一起使用。将表单张贴到示例php脚本的url中。删除此行:
$q = urlencode('Toy Story');
替换为:
$q = urlencode($_POST['q']);
这应该会让你开始。
请注意,没有对$_GET进行任何清理。我只是提供了您需要的基本信息,以便根据您发布的示例代码运行搜索表单。
如果你不知道如何创建表单,这里有一个教程链接:
http://www.tizag.com/phpT/forms.php