Jump to content

t_v

New Members
  • Posts

    5
  • Joined

  • Last visited

t_v's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. What exactly makes it poor code - the whole thing, or making a variable global in the function so it can be accessed outside?
  2. 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.
  3. 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.
  4. 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.
  5. It said there was an error with the application or that the permissions weren't right (one for each I can't remember exactly as this was yesterday). Also, I had to enter the Captcha like 10x just to get it right.
×
×
  • 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.