-1

I am making some kind of website to watch movies/tv shows, just for fun.And I am stuck, basically I made function getMovieFromAPI that takes given ID and searches inside database for imdbID, then I use that imdbID to find info about that movie from API.And everything is working great, I can use echo inside funcion to show data.My question is, how can I use this data outside of this function, example: when I want to see year and plot of movie how can I get that data from this function.I tried returning it as array and all other stuff.I hope I explained well what I want

My code:

function getMovieFromAPI($IDD) {

global $connect, $omdbAPIKey;

$query[0]   =  "SELECT * FROM movies WHERE ID = '$IDD' LIMIT 1";
$results[0] =  $connect -> query( $query[0] );
$row[0] = $results[0] -> fetch_assoc();

$ID             =   $row[0]['imdbID'];

$apiurl = 'http://www.omdbapi.com/?i='.$ID.'&apikey='.$omdbAPIKey;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response);}

Edit: as requested, I made test page just to call this function and see if it works

getMovieFromAPI('1');
Medis Redzic
  • 105
  • 1
  • 6
  • Can you edit your code to let us know where you are calling your `getMovieFromAPI()` function? Where would a return value be returning to? Also, are you having trouble parsing the JSON object to get the data inside it? – Enigmadan Aug 11 '17 at 17:45
  • Are you using this in website or mobile app? – Vinay Aug 11 '17 at 17:52
  • On website @Novice – Medis Redzic Aug 11 '17 at 18:08
  • @Enigmadan I added it, I really don't get part about having trouble with parsing the JSON object, I am new to this so sorry – Medis Redzic Aug 11 '17 at 18:09
  • @Medis Redzic Looks like your problem is trivial as converting the array into meaningful html I never worked on Omdb so dont know exact data/format it returns can you post the returned json? – Vinay Aug 11 '17 at 18:15
  • @MedisRedzic I tried to explain the process from start to finish in my response. Hopefully it helps! – Enigmadan Aug 11 '17 at 18:32
  • @Novice thank you for help, I figured it with Enigmadan post. – Medis Redzic Aug 11 '17 at 18:42

1 Answers1

1

Some basic function stuff

In order to make use of an object you create in a function, you really have two options. You can either pass the information outside of the function somehow, or initialize the object outside of the function.

The first option can be done using a return statement. This will return the object to the place the function is being called. If you want to use the returned data, you'll need to store it somewhere:

// This function's return value isn't actually being stored anywhere
thisFunctionReturnsSomething($a, $b);

// This function's return value is being stored into the variable $data
$data = thisFunctionReturnsSomething($a, $b);

But we also need to make sure our function is returning a value so we can use it!

function thisFunctionReturnsSomething($a, $b){
  // Lets do something with the variables that are passed in
  $c = $a + $b;
  // And return what we just did
  return $c;
}

Now if we call our function and store the return value $data = thisFunctionReturnsSomething($a, $b); we'll have the sum of $a and $b stored in $data.

Some JSON stuff

Once you have your data, you can use the JSON's hierarchical architecture to reach the specific information you want.

Say you have a return value that looks like this:

$data = {"key1": "value1", "key2": "value2"};

You can use the following to access the specific values you want:

echo $data->key1; // This will return value1

However, it would be easier to use an array to access values as explained here

$json = '{ "results" : [ { "items" : 
            [ { "item" : [ { "url" : "http://google.com/"} ] },
              { "item" : [ { "url" : "http://stackoverflow.com/"} ] } 
            ],
         } ] }';
$data = (array)(json_decode($json)); // Casts JSON object to an array
// Get the data from the nested array
echo $data['results'][0]->items[1]->item[0]->url;

That echo statement requests the first index ([0]) of the results object and then goes further in to the items object, grabs the second element in that array and returns the value of the url item. This should return the url http://stackoverflow.com/

Community
  • 1
  • 1
Enigmadan
  • 3,398
  • 2
  • 23
  • 35