从Apple请求视频

时间:2015-12-18 04:44:28

标签: php ajax codeigniter api

我是一个完整的编程新手,正在研究一个示例项目。我正在尝试向Apple发送一个AJAX请求,以获取用户定义的艺术家的视频。但是,我没有将任何数据返回到我的函数中,并且没有任何内容显示在控制台中。

的index.php

<!DOCTYPE html>
<html>
<head>
    <title>iTunes Video Search</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('form').submit(function() {
                $.post($(this).attr('action'), $(this).serialize(), function(res) {
                    var htmlStr = '';
                    if (res.results.length !== 0) {
                        htmlStr += "<video controls src='" + res.results[0].previewUrl + "'></video>";
                    } else {
                        htmlStr += "Not Found";
                    }
                    $('#video').html(htmlStr);
                }, 'json');
                return false;
            });
        });
    </script>
</head>
<body>
    <h2>Enter Artist's Name</h2>
    <form action="/videos/create" method="post">
        <input type="search" name="artist_name" id="artist_name">
        <input type="submit" value="Search">
    </form>
    <div id="video"></div>
</body>
</html>

Videos.php

<?php

class Videos extends CI_Controller {

    public function index() {
        $this->load->view('index');
    }

    public function create() {
        $artist_name = str_replace(' ', '', $this->input->post('artist_name'));
        $url = "https://itunes.apple.com/search?term=" . $artist_name . "&entity=musicVideo";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $data = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        echo $data;
    }
}

?>

非常感谢任何帮助!

0 个答案:

没有答案