Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

Help with an PHP API integration

i'm trying to integrate the bits on the run PHP API , but i need some help to figure out how to integrate the Videos into the website i'm trying to build . SeeAPI what i want to accomplish is : be able to retrieve a video based on their unique ID .So if somebody can help me let me know.

25 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Have you taken a look at the PHP examples? I haven't worked with this particular API before, but it looks pretty straightforward. The /embed-code/index.php example looks like the one you'll want. Looking at the documentation, I think this is the method you need:

It lets you pass in a video key and returns data about the video.

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

Yeah i was checking it out , but one thing that i was trying to is , when user click on the anchor link ,the link will have a query string that will request this particular song base on the video key associated with this particular video clip. the reason why i have some difficulties , it's something that I'm doing by learning , i never build anything in PHP before. and the irony is , this things is basically the app.I'm gonna try again see if i can come up with something.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Could you share some of the code you've been using? Maybe link to an example of running?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

@Randy , let me share the controller that display all the videos on the page and the html template that render the videos .

Controller: <?php include('inc/db_connect.php');

try { $sql = "SELECT ID, URL, GroupName, VideoTITLE, new, song_id FROM videoclip "; $result = $pdo->query($sql); } catch(PDOException $e) { $error = 'unable to fetch data: '.$e->getMessage(); include'error.html.php'; exit(); } $URLS = array(); while ($row = $result->fetch()) { $URLS[] = array('id' =>$row['ID'],'new' =>$row['new'], 'URL' => $row['URL'], 'GroupName' => $row['GroupName'], 'VideoTITLE' => $row['VideoTITLE'],'song_id' =>$row['song_id'] ); }

$song_id = $_GET[$row['song_id']]

Template: <div class="container-fluid">

  <div id ="col"  class="span2 visible-desktop">
      <?
    if (isset($_SESSION['user_id'])) {?>
      <div class="myaccount">

       <h5 class="first"><i id="act_btn" class="account_icon icon-user icon-white"></i>My Account</h5>
          <ul class="account_nav nav nav-list">
            <li><a href="myaccount.php"><i class="account_icon icon-user"></i>Account info</a></li>
            <li><a href="mysettings.php"><i class="account_icon icon-cog"></i>Settings</a><span  data-placement="right" data-title="Notification List" data-content="test" class=" notify badge badge-success pull-right">2</span></li>
            <li><a href="#"><i class="account_icon icon-globe"></i>Notifications</a></li>
            <li><a href="logout.php"><i class="account_icon icon-hand-right"></i><!--<img class="account_icon" src="img/social/login.png" alt="login">-->Log out </a></li>
          </ul>
      </div>
      <? } ?>
  </div>

        <div id="inner" class="span9">

          <ul class="thumbnails">
            <?php foreach ($URLS as $URL){

                    echo '<li class="span3">';

                    echo '<div class="thumbnail">';

                    echo '<img src ="' . $URL["URL"] . '" alt="'. $URL["GroupName"] . '">';

                    echo '<h3 class="title">' . $URL['GroupName'] . '</h3>';

                    echo '<p class="song">' . $URL['VideoTITLE'].'</p>'; 

                    echo '<div class="thumbs">'.ThumbsUp::item($URL['id'])->options('align=right').'<div>';

                    echo '<a class="bigbtn" href="'.'videoview.php?'.'song_id='.$URL['song_id'].'"></a>'; 

                    echo '</div>'; 
                    if(isset($URL['new'])== 1 )
                         {
                    echo '<div class="new">Premiere</div>';
                       } 
                    else
                    echo "";                  

                    echo "</li>";

                  }

            ?>
          </ul> 



          </div>

      </div> 
  </div>

</body> </html>

let me explain my issue better what i want to do is , the same way you went to VeVo and click on the play Button and the system went and retrieve the correct video and display, i don't know how to implement it .each song has a unique key on the content server , so in my database i have a song_id that i want to mach the videokey on the content server when i click on the anchor link it will submit a Get request that will pull the correct video based on the key associated with the song_id , Hope i'm making some senses.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Is the template page working fine? Can someone click on one of the thumbnails? If they do, do they end up at a page with a web address like this?

videoview.php?song_id=5

What does that code on that page look like?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

yeah , they end at the page with the address like that , but i want the song_id to be match with the Key they have on the server ,when someone click on it, he will see the correct video.

Template for the Videoview,php include 'inc/db_connect.php'; include 'inc/header.html'; include 'inc/browse_controller.php'; require_once('botr/api.php'); $botr_api = new BotrAPI('pcV37m19','vJipxyJ4QoBVyhKF8DDgbckE'); $response = $botr_api->call("/videos/show",array('video_key'=>$key)); $video_key = $_GET[$array[1]['key']]; echo $video_key ?>

?> <body class="videoview">

<div class="container">
    <div class="span12">

        <div class="video_player">
<iframe src="http://content.bitsontherun.com/players/Sod9EWVc-jVq0TSuG.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>

        </div>
    </div>
</div>

</body> </html>

if you go to this page . www.mizikbox.com/browse.php click the play button you will see what im trying to do

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It sounds like you need to get the video keys from Bits on the Run and store them in the database. Does that sound like it will work?

If so, then the question becomes: How do you get the video keys from Bits on the Run into the database? It depends on this:

  • How many videos do you have?
  • Are new videos ever added? If so, how often are new videos added?
  • (if new videos are ever added) When you add a new video to Bits on the Run, how does the data for that video get loaded into to the database? (Does the person who added the video to Bits on the Run also add it to the database at the same time?)
Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

thanks for helping me , yes this is actually what i need in order to retrieve the correct videos on the click event. Have a way to communicate with the API to retrieve the Key for

Right now because I'm in development i have 9 videos in the Bits on The run server .but when everything is fully working , probably it's gonna be more than 200 videos.

yes new videos will be added , probably when there's a new release . may be 2 or 3 in a months. The process is we add everything in the database and we only store the videos in the content server. that means when i have a new video . i enter it first in the database with all the info and poster link for the videos . so the song_id will play the role of a link between the API and the database. the same person add both information.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

If the same person is putting the video into Bits on the Run and into the database, then I don't think there's a need to use the API. When you add the video to Bits on the Run, find the video key there and copy it. Then go create the video in the database and manually add the video key there into a column there.

(You would want to use the API if you wanted to load the videos from Bits on the Run into the database automatically. Let's say someone added a video in Bits on the Run. You could have code on your server -- not the controller, but a separate job that ran on a schedule -- that connected to the API, looked up all the videos, figured out which videos were new, and then loaded the new ones into the database automatically.)

Does that help?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

yeah , that what i was thinking at first , i try to copy the key from the server and add into the database and i try to insert the key into the iframe by concatenate it with the script they provide , it was not working . it gave me a file not found issue .

here's an example

$song_id= $_Get($row[song_id])

   <iframe src="http://content.bitsontherun.com/players/."$song_id."-jVq0TSuG.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>

the automatically process is something i'm thinking to do for the next version.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I think you want that code in videoview.php to be this:

$song_id = $_GET["song_id"];

The videoview.php page isn't looking up the key from the database; it's loading the key straight from the query string. If the web address is this ...

videoview.php?song_id=@@@@@

... then this code ...

$song_id = $_GET["song_id"];
echo '<iframe src="http://content.bitsontherun.com/players/' . $song_id . '-jVq0TSuG.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>';

... should output this ...

 <iframe src="http://content.bitsontherun.com/players/@@@@@-jVq0TSuG.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>

Does that help?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

@ Randy , just confuse .

My anchor link to launch the videoview.php

echo '<a class="bigbtn" href="'.'videoview.php?song_id="'.$song_id.'">'.'</a>';

in the Videoview.php

$song_id = $_GET["song_id"];

echo '<iframe src="http://content.bitsontherun.com/players/' . $song_id . '-jVq0TSuG.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>';

do you think it's correct cause it doesn't work

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

If I go to browse.php and hover over the first video, the link is this:

http://www.mizikbox.com/videoview.php?song_id=

It looks like there's a problem with browse.php. In that file, it looks like $song_id is blank. If you are still using the view earlier, it probably needs to be this:

echo '<a class="bigbtn" href="'.'videoview.php?song_id="'.$URL['song_id'].'">'.'</a>';

Does that help?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It looks like there's a problem with browse.php. If I go to browse.php and hover over the first video, the link is this:

http://www.mizikbox.com/videoview.php?song_id=

In that file, it looks like $song_id is blank, so the link to videoview.php is wrong. If you are still using the view code from earlier, it probably needs to be this:

echo '<a class="bigbtn" href="'.'videoview.php?song_id="'.$URL['song_id'].'">'.'</a>';

Does that help?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

ok like you said The videoview.php page isn't looking up the key from the database; it's loading the key straight from the query string. so i setup the videoview ,php like that

<?php
echo '<iframe src="http://content.bitsontherun.com/players/' . $song_id . '" width="940" height="540" frameborder="0" scrolling="auto"></iframe>';
?>

but when i view the page source nothing was outputed.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I think you are misunderstanding the problem. Walk through these steps to see the problem clearly:

  • Go to the browse.php page: browse.php
  • In the browser, go to View > View Source to see the HTML on the browse.php.
  • Scroll down to line 204 to see the first link that the browse.php file generates.
  • Take a look at the value of the href for that first link. Notice that it is missing the video key. It is just this:

...

<div><a class="bigbtn" href="videoview.php?song_id="></a></div>

That's the problem. The browse.php page is the page that's broken. It generates links to the videoview.php page, but those links are wrong; those links do not have the video key.

Does that help?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

@randy can i send you the whole file , if you can get a look at it , it's been 4 days been looking at it and i can't fixed , probably you will see the mistake i made.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher
  1. Post the code online somewhere and add a link here, and I'll try to take a look.
  2. Could you also send me an example of a video key?
Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(1) First question: do you have the video keys in the <code>song_id</code> column of the database?

(2) I can see that you have made a few changes to browse.php based on this thread. It hasn't quite ended up correct. You have this line:

  echo '<a class="bigbtn" href="'.'videoview.php?'.$song_id=$URL['song_id'].'"></a>' ;

This is not doing what you intend it to do. (You don't mean to be assigning a variable in the middle of echo'ing out that string.) It should probably say this:

      echo '<a class="bigbtn" href="'.'videoview.php?'.$URL['song_id'].'"></a>' ;

Once you make that change, I believe the links to videoview.php will give you something to work with.

Let me know!

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

i don't have all video key in the database yet , i was implement 2 first to see how it works .

Randy , the real issue for me right ,

how do i retrieve the video key in the "iframe tag". that's the prob . when i put the $song_id variable. when i see the source nothing echo.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

You'll need to get the browse.php file fixed as I described above. Then you fix the videoview.php. Find this block of code --

<?php
    echo '<iframe src="http://content.bitsontherun.com/players/' .$song_id.'".html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>';
?>

-- and add the following to it --

<?php
    $song_id = $_GET["song_id"];
    echo '<iframe src="http://content.bitsontherun.com/players/' .$song_id.'".html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>';
?>

That will work, though there's more you need to do to make it secure.

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

it works , Thanks so much i appreciate , there was some issue with concatenation in the videoview so i break it down like

<?php
        $song_id = $_GET["song_id"];
        $url = "http://content.bitsontherun.com/players/";
        $src =$url . $song_id;
        echo '<iframe src="'.$src.'.html" width="940" height="540" frameborder="0" scrolling="auto"></iframe>'
        ?>