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

09-24-2010, 03:03 PM
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

$m = new manage($_POST['entry_id']);

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");

09-24-2010, 03: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:


switch ($_POST['action'])
case 'delete':
include 'delete.php';

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.

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

09-24-2010, 03: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 >.<