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. 

 

 

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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 by t_v
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.