t_v Posted February 5, 2015 Share Posted February 5, 2015 I'm using multiple API's, all of which get called using cURL. Someone on Reddit recommended code that looks like this: function fetch($url, $the_id = null) { // Initiate the cURL fetch $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Send authorization header with the the ID. Without this, the query won't work if ($the_id) curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$the_id)); $result = curl_exec($ch); curl_close($ch); // Put the results to an object return simplexml_load_string($result) or die("Error: Cannot create object"); } // Without auth id $test = fetch('http://forums.phpfreaks.com'); // With auth id $test = fetch('http://forums.phpfreaks.com', 't_v'); But when I then use the foreach to pull the data from the API, it produces no results. foreach($result->things->children() as $thing) { echo "<p>". $thing->cool_thing ."</p>"; echo "<p>". $thing->cooler_thing ."</p>"; } Whether I use echo fetch(parameters); or fetch(); it does get any results, unlike if I used it outside of a function. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 5, 2015 Share Posted February 5, 2015 Your code is inconsistent: are you trying to foreach on $result (which is a string) or $test (which is a SimpleXMLElement). Assuming the latter, have you verified that $result is the XML string you expect it to be? And that it looks something like <foo> <things> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> </things> </foo> Quote Link to comment Share on other sites More sharing options...
t_v Posted February 5, 2015 Author Share Posted February 5, 2015 (edited) Your code is inconsistent: are you trying to foreach on $result (which is a string) or $test (which is a SimpleXMLElement). Assuming the latter, have you verified that $result is the XML string you expect it to be? And that it looks something like <foo> <things> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> <bar> <cool_thing>A cool thing</cool_thing> <cooler_thing>A cooler thing</cooler_thing> </bar> </things> </foo> It works when I run the foreach after cURL when not used in a stored function. So if I use this: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Send authorization header with the CJ ID. Without this, the query won't work curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$the_id)); $result = curl_exec($ch); curl_close($ch); // Put the results to an object $resultXML = simplexml_load_string($result) or die("Error: Cannot create object"); foreach($resultXML->things->children() as $thing) { echo $thing->something; echo $thing->another_thing; } I just want to stop repeating myself in the code as that's a better practice. I'm using multiple API's where the cURL statement is very similar and I figured I should use a function or method. Edited February 5, 2015 by t_v Quote Link to comment Share on other sites More sharing options...
requinix Posted February 5, 2015 Share Posted February 5, 2015 And so function fetch($url, $the_id = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Send authorization header with the CJ ID. Without this, the query won't work curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$the_id)); $result = curl_exec($ch); curl_close($ch); // Put the results to an object $resultXML = simplexml_load_string($result) or die("Error: Cannot create object"); foreach($resultXML->things->children() as $thing) { echo $thing->something; echo $thing->another_thing; } } fetch($url, $the_id);does not work? Or are you trying function fetch($url, $the_id = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Send authorization header with the CJ ID. Without this, the query won't work curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$the_id)); $result = curl_exec($ch); curl_close($ch); // Put the results to an object return simplexml_load_string($result) or die("Error: Cannot create object"); } $resultXML = fetch($url, $the_id); foreach($resultXML->things->children() as $thing) { echo $thing->something; echo $thing->another_thing; }Or else what is your complete code that isn't working? Quote Link to comment Share on other sites More sharing options...
t_v Posted February 6, 2015 Author Share Posted February 6, 2015 At this point it is working, thank you. The XML has different names for each of the 3 different API's, so I before I didn't put the foreach into the function. I still didn't need to, because I added this to the function: global $returnXML; This allowed me to rely on that same variable for all three API calls outside of the function. fetch($api_url1); echo "some result: ".$returnXML->items->item->name; fetch($api_url2); echo "results from a different API: ".$returnXML->things->something; By any chance though, does making a variable global make it less safe? I'm the only one who codes on my site. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 6, 2015 Share Posted February 6, 2015 It's poor code, but it's not necessarily less safe. Quote Link to comment Share on other sites More sharing options...
t_v Posted February 6, 2015 Author Share Posted February 6, 2015 It's poor code, but it's not necessarily less safe. What exactly makes it poor code - the whole thing, or making a variable global in the function so it can be accessed outside? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 6, 2015 Share Posted February 6, 2015 The global variable. Functions have arguments and return values and you should be using them, not creating global variables that magically exist without any obvious indication as to why or how. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.