Jump to content

I'm struggling to get cURL into a stored function.


t_v

Recommended Posts

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. 

 

 

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>

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. 

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?

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. 

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.