tags:

views:

83

answers:

3

I'm building up a url to execute with curl. the url will call an api for the LMS that I'm using. Before being able to call anything else, you need to receive a token from the LMS to put in the url. I have gotten the token from the api, and I can echo it and it shows up just fine, when I echo the url after appending the token to it, it doesn't show up.

curl_setopt($c, 'CURLOPT_RETURNTRANSFER', true);
$res = curl_exec($c);
curl_close($c);

$start = strpos($res,"<token>");
$end = $start+37;
$token = substr($res,$start+7,$end-$start);

echo "{$token}<br />";

$url = "/www/api2.php?action=create_user";
$url .= "&login=" . urlencode($username);
$url .= "&password=" . urlencode($password);
$url .= "&name=" . urlencode($data['first_name']);
$url .= "&surname=" . urlencode($data['last_name']);
$url .= "&email=" . urlencode($email);
$url .= "&languages=english";
$url .= "&token=" . $token;

echo "{$url}<br />";

Output of the echo "{$url}<br />";

/www/api2.php?action=create_user&login=foobar3130&password=6116b3f29c&name=Foo&surname=Bar&email=foobar%40gmail.com&languages=english&token=

Output of echo "{$token}<br />";

pUCu2BUAE1heAyQ93fApfhvDE1bjKd

Edit I added a check to see if $start was false, and it is false. I guess its not actually the token that gets echo'd because if I comment that line out, the string that I have for the token output still gets printed. I'm not sure what it would even be from.

Edit 2 I now have it returning xml but I am not sure how to parse it to get the token. It returns:

<xml><token>Fp1rYkds4fSuTAQxTvLvSiW5NE2FJz</token></xml>
+1  A: 

try:

ob_start();
echo "/www/api2.php?action=create_user";
echo "&login=" . urlencode($username);
echo "&password=" . urlencode($password);
echo "&name=" . urlencode($data['first_name']);
echo "&surname=" . urlencode($data['last_name']);
echo "&email=" . urlencode($email);
echo "&languages=english";
echo "&token=" . $token;
$url=ob_get_contents();
ob_end_clean();

echo $url;
stillstanding
+1  A: 

From what your script shows, you are trying to parse the Token out of an XML result, with a fixed width of 37 characters.

Judging from your latest comment, that's where the problem lies.

A much better approach would be to use actual XML DOM parsing to get the token from the file.

Pekka
Could you possibly give an example? I'm having trouble getting the DOMDocument stuff to work.
The.Anti.9
@The you'll need to show your XML.
Pekka
Oh yes, my mistake. I have included in my post now.
The.Anti.9
+3  A: 

Your edit seems to confirm that curl_exec() isn't returning the data to you - it is sending it directly to the browser. Use the option CURLOPT_RETURNTRANSFER to have it return the value to your variable.

jasonbar
I agree. So just add this line before you call curl_exec(): curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
Fox
This line was already in the script, I just didn't have it in the code I pasted here.
The.Anti.9
@The.Anti.9, what is the output of `var_dump($res);`?
jasonbar
I have included what it returns in my newest edit.
The.Anti.9
This is, of course, the right answer. I thought I had done it correctly, but as it turns out, i had CURLOPT_RETURNTRANSFER in quotes as you will see in my code. This, obviously, makes it not work correctly.
The.Anti.9