...

View Full Version : How does curl work when programming an api?



earni23
09-24-2010, 04:03 PM
Hi
I just read this article (http://particletree.com/features/how-to-add-an-api-to-your-web-service/) which is about making an api with php. The person who wrote it has created a file as follows:

file: api/delete.php



require_once('manage.php');
if(hasPermission($_POST['api_key'])
{
$m = new manage($_POST['entry_id']);
$m->deleteEntry();
}


And when he uses curl, he wrote like this:



$ch = curl_init("http://site.com/api/select");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);


My question is how does the api know that it should call the delete.php file when he is using select in curl_init(...)?
Shouldnīt it be: $ch = curl_init("http://site.com/api/delete.php");

Fou-Lu
09-24-2010, 04:14 PM
I guess it depends on what is in $request.
SELECT could be a centralized page that runs and detects the action to perform, something like:


<?php

switch ($_POST['action'])
{
case 'delete':
include 'delete.php';
break;
...
}
?>


The article does indicate that the user should attach to delete.php though:


This will allow users to send a POST request to us at http://site.com/api/delete.php with an api_key and an entry_id.


My personal preference is SOAP which is more robust than rest.

earni23
09-24-2010, 04:21 PM
But if "SELECT could be a centralized page" shouldnīt Select end with a ".php"

Fou-Lu
09-24-2010, 04:42 PM
But if "SELECT could be a centralized page" shouldnīt Select end with a ".php"

That would depend on the rules set up by the htaccess (if available).
As it sits right now you are right, using that should try to access /api/select/index.php if that's the default type to serve. This differs from the SOAP approach (sorry, gots a little confused first thing in the morning :P), where your action is dictated in a fashion very similar (/service/delete for example).
Took a quick read over the article, but I can't go through all the comments. Its possible its a typeo >.<



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum