Saturday, June 26, 2010

curl post vs. normal post?

Situation: I'm building an API with cakePHP which holds user-accounts.
I'd like to have multiple websites authenticating using this API by
sending a post-request to the api and receiving the proper xml with
user-info.

To accomodate routing, I've added the following rule in routes.php.

Router::connect('/:channel/:version/:controller/:action/*',
array('prefix' => 'api', 'api' => true),
array('channel' => '[0-9]+'),
array('version'=>'[0-9]+\.[0-9]+'));

I've also created a form that holds all the necessary information
(username + password) and tested this. This works. However, I'd like
to use cURL to form a post-request & receive the information. For
this, I created the code below.

$api_url = 'http://localhost/api-test/1/0.2/users/auth.xml';
$api_token = 'xxxxxxxxxxxxxx';
$url = $api_url.'?token='.$api_token;
$data = array('user' => $user, 'password' => $password);
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);

This strangely doesn't work. The connection is working, but the
routing appears to be failing, as the controller 'api-test' is called
(which doesn't exist, this is my main-folder of my test-application)
instead of the user-controller. I'm sure I must be missing something,
but I can't figure out what that is.. I'm sure it's something between
cURL and cakePHP, as a normal form works as intended. Anyone got any
advice?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: