Jump to content

How Can I Store The Returned Result Of A Function?


leke

Recommended Posts

How can I store the returned result of a function that outputs an array, so I can pass parts of it to other functions? As you can se, this function will have different output each time it is called as a function.

 

Thanks.

 

<?php

$apikey = '1234567890';
$movie_title_file = "5000-movies.txt";
$access_movie_title_db = file_get_contents($movie_title_file);
$movie_title_db = explode("\n", $access_movie_title_db);

function getMovieObject() {

global $apikey, $movie_title_file, $access_movie_title_db, $movie_title_db;
$random_index = array_rand($movie_title_db,1);
$query = $movie_title_db[$random_index]; // end result is a film title to query.
$url_title = urlencode($query); // make sure to url encode an query parameters
// Taken from the RT API...

// 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=' . $url_title;
// 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');
$movies = $search_results->movies;

return $movies;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.