mofle Posted April 10, 2008 Share Posted April 10, 2008 Hi, i need to do a loop of the array of this function found in the WebUI API: public function getTorrents() WebUI API: http://pastebin.com/m76205d0b I have this one, but it doesn't work. $utorrent = new uTorrent(); $utorrent->host = $_SESSION['host']; $utorrent->user = $_SESSION["user"]; $utorrent->pass = $_SESSION["pass"]; $torrents = $utorrent->getTorrents(); foreach($torrents as $torrent) { echo $torrent[uTORRENT_TORRENT_NAME]."\n"; } How do i use the "public function getTorrents()" to get the array into a foreach loop? I really appreciate your help Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/ Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 It seems like it's returning JSON. You can use json_decode() to turn it into an array. Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514109 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 Only thing I see is this function json_decode($req, true) Where is it declared?? Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514113 Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 Only thing I see is this function json_decode($req, true) Where is it declared?? It's a built-in function: http://php.net/json_decode Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514116 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 OK smack me around a bit. Never run into that one Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514117 Share on other sites More sharing options...
mofle Posted April 10, 2008 Author Share Posted April 10, 2008 It seems like it's returning JSON. You can use json_decode() to turn it into an array. Yes, take a look at this bit of the WebUI at the bottom: it gets and decodes the json, what i need i a method to get the array from "public function getTorrents()" and get it into a foreach loop. Do you think you can help me with that? // performs request private function makeRequest($action, $decode = true, $options = array()) { $ch = curl_init(); curl_setopt_array($ch, $options); curl_setopt($ch, CURLOPT_URL, "http://".$this->host."/gui/".$action); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $this->user.":".$this->pass); $req = curl_exec($ch); curl_close($ch); return ($decode ? json_decode($req, true) : $req); } Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514122 Share on other sites More sharing options...
Daniel0 Posted April 11, 2008 Share Posted April 11, 2008 Well, if it returns an array then it should work in a foreach loop. Try to print_r() the array it returns and post the output here (if it's large, cut out some of it). Link to comment https://forums.phpfreaks.com/topic/100516-foreach-loop-with-the-array-of-a-public-function/#findComment-514413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.