0

I am new to using api's and php. but i am trying to get get a video from within my my project directory using a "new video" button on my simple page. I am not able to get the new video to play. It looks to be a syntax error on my end but i am unsure where i am making the mistake. Also I am using slim v2 framework, if that makes a difference.

    <!DOCTYPE html>

<html lang="en">
    <head>
        <title>WOO</title>
        <style type="text/css">
            .center {
                margin-left: auto;
                margin-right: auto;
                margin-top: 0px;
                width: 70%;
            }
            h3 {
                margin-left: auto;
                margin-right: auto;
                margin-bottom: 0px;
                width: 70%;
                font-family: "Lucida Console", Monaco, monospace;
            }
        </style>
    </head>
    <body style="background-color: honeydew;">
        <h3 id="video-title">Video Example <button id="do-it">new video</button></h3>
        <div class="center">
            <video id="game" height="600" width="800" controls>
                <source src="videos/cyanide.mp4" type="video/mp4" />
                You can't play this video
            </video>
        </div>
        <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
        <script type="text/javascript">
            $('#do-it').on('click', function() {
                var video = document.getElementsByTagName('video')[0];
                var source = video.getElementsByTagName('source')[0];

                $.ajax({
                    'url'       : 'php/api.php/getVideo?search=anthony',
                    'method'    : 'GET',
                    'dataType'  : 'json'
                }).done(function(data) {
                    console.log(data);
                    source.src = data.src;
                    $('#video-title').text(data.title);
                    video.load();
                })
            });
        </script>
    </body>
</html>

Here is the full html scrpit and below is my php script

<?php
require 'vendor/autoload.php';

$app = new \Slim\Slim();

// If the url matchs a certain word, it'll go to the function specified
$app-> get('/getTest','getTest');
$app-> get('/getVideo','getVideo');
$app->run();

function getTest () {
    echo '{"woo":' . json_encode('we made it') . '}';
}

function getVideo() {
    // get params from query string
    $app = \Slim\Slim::getInstance();
    $search = $app->request()->params('search');

    // get video data from search

    $video = (object)array("title" => "CLUTCH AD", "src" => "videos/anthony_davis.mp4");
    echo json_encode($video);
}
Cole M
  • 1
  • 2
  • Please show the relevant HTML. Also, have you looked in the debug console to see what script errors are reported? Off the top of my head, I'm guessing that a relative path for your ajax URL is a problem. Also, do you know if your PHP script is getting executed or not? – jfriend00 Mar 12 '16 at 18:49
  • It is saying that my request is not found. – Cole M Mar 12 '16 at 19:11

1 Answers1

0

If the PHP request is not being found from the browser, then it is likely that this relative URL:

'php/api.php/getVideo?search=anthony'

is not correct. We don't know the file hierarchy of your page or the script, but I'd suggest you first try using a fully qualified URL including hostname and full path to get it working. If there's some reason to then use a relative path from there, you can then modify it while starting from a working script. To get it working switch to a fully qualified URL including the http://:

'http://somedomain.com/somepath/php/api.php/getVideo?search=anthony'
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I am a little unsure what you are saying to do. I am fairly new to this, could you maybe explain a little more? – Cole M Mar 12 '16 at 19:41
  • I'm saying to use a fully-qualified URL when trying to make the PHP request. Fill in the `somedomain.com/somepath` part of the example URL in my answer and make sure it starts with the appropriate `http://` or `https://`. See [here](http://stackoverflow.com/questions/17406848/what-is-the-definition-of-an-absolute-url-fully-qualified) if you still don't understand what a full-qualified URL or an absolute URL is. – jfriend00 Mar 12 '16 at 19:42
  • The error is saying that my $('do-it') function is an unresolved function. I tried giving a fully-qualified URL and it did not fix the problem. – Cole M Mar 12 '16 at 22:25
  • @ColeM - What is an unresolved function? Please post the EXACT verbatim error message and tell us whether you're seeing this in the browser or in your PHP. You have to be very specific when describing what you see and the error messages you see. That's the only way we can understand enough to have a chance to help you. – jfriend00 Mar 12 '16 at 22:27
  • Also when i enter the exact URL in my browser it is just giving me the 404 error. – Cole M Mar 12 '16 at 22:30
  • @ColeM - Then you need to figure out the right URL to your PHP file or you need to fix something on the back-end to make the desired URL work. Not much we can do here as that's all your back-end configuration which you haven't told us anything about. Making sure it works in the browser first is a good test. Until that works, no point in working on the script. – jfriend00 Mar 12 '16 at 22:33
  • If `$('do-it')` is an unresolved function, then jQuery is not loading succesfully. Your jQuery URL in your web page looks correct to me so I don't know why it wouldn't be available unless it is blocked because it's an https URL, but your page is only http. You could try changing the jQuery URL to `//code.jquery.com/jquery-2.2.1.min.js` which will load it from the same protocol as the page and prevent a mismatch. – jfriend00 Mar 12 '16 at 22:44