simple Services parameter question

Events happening in the community are now at Drupal community events on www.drupal.org.
perikut's picture

hi,
I'm newbie to Drupal and I need it specifically for feeding iPhone apps with JSON data.
I know how to access dbase data via a module using node_load and how to generate the JSON.

Now I simply want to pass a parameter in the URL, in order to get only the info that I want (ex: http://...mymodule?param=X)

I'm completely lost about how to do it. I have checked various drupal resources without getting anything clear. In most cases the info is not referering to Services.

Can someone please point out some ideas, resources, documents....
thanks in advance,

Pere

Comments

Hi perikut, if you're using

rolf vreijdenberger's picture

Hi perikut,

if you're using drupal 7, you can use the Rest Server in the services module.
you would get an url like: example.com/path_to_rest_endpoint/user/1.json
which would retrieve ("get") user 1 in json format.

the format you would like to have in your url is just a different format of what is mentioned above, with the difference that it is not restful.
Any reason why you would specifically like to have it that way?

cheers

Kind regards,

Rolf Vreijdenberger

drupal and flash: http://www.dpdk.nl/opensource/drupalproxy-as-a-bridge-between-flash-as3-...

services parameteres question... via cURL?

perikut's picture

hi Rolf,

thanks for the answer.

I'm currently not using Drupal 7 but Drupal 6. I need a list of nodes info (i'm not sure if REST server can handle multiple nodes at the same request?). About services, would you recommend me to upgrade to Drupal 7? (I'm just starting)

I'm currently creating a JSON using the code below; I would like to make a very simple thing: pass a parameter to this Service module, so that the info can be classified depending on user request. I assume this param can be sent on the URL calling the method, so I can get it in the module using $_GET['my param']

I'm trying to test using cURL, but I suppose that before calling my method I have to autenticate, right?

Following the code below I get a 'cannot login' message; which could be the reasons for that? permissions maybe? in error_drupal.log i get a 'closed keepalive connection'; cURL works, as I can see the HTML outside Drupal, but authentification fails.

I have followed http://drupal.org/node/322530 and http://drupal.org/node/86727

If I could finally log in, how could I call the module via URL? could I add to this URL the parameter I need? ex: http://...../mymodule?param=X

thank you very much for the patience!

DRUPAL LOG IN CODE

$crl = curl_init();
$url = "http://MYDRUPAL/user/login";
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($crl, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_POST, 1);

// this array will hold the field names and values
$postdata=array(
"edit[name]"=>"MYUSERNAME",
"edit[pass]"=>"MYpassword",
"edit[form_id]"=>"user_login",
"op"=>"Log in"
);
// tell curl we're going to send $postdata as the POST data
curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);
$result=curl_exec($crl);
$headers = curl_getinfo($crl);
if ($headers['url'] == $url) {
die("Cannot login.");
}

MY MODULE (feature_service.module)

function feature_service_service()
{

$items= array(
'#method' => 'feature.all',
'#callback' => 'feature_service_all',
'#return' => 'array',
'#help' => 'Returns a list of features 5',
'#access callback' => 'feature_service_access'
);

return $items;
}

$output = array();
$parsed = array();

function feature_service_all()
{
// Image Galleries
$result = db_query("SELECT nid FROM {node} WHERE type='images'");
while ($node = db_fetch_object($result))
{
$nodeAll = node_load($node->nid);
$photoArray = $nodeAll->field_image;
$classifArray = $nodeAll->field_classificacio;

   $photoObjects = array();
 $info=array();
 foreach ($classifArray as $class)
 {
 $classification=$class['value'];
 if ($classification=='xocolata' || $classification=='gelat')
 {
    foreach($photoArray as $photo)
    {
        $filename = $photo['filename'];
        $filepath = $photo['filepath'];
        $data = $photo['data'];
        $title = $data['title'];
        $photoObject = array();           
        $photoObject['classif']=$classification;
        $photoObject['filename'] = $filename;
        $photoObject['filepath'] = $filepath;
        $photoObject['title'] = $title;
        $photoObjects[] = $photoObject;       
    }
  $info['photos'] = $photoObjects;
    $parsed[] = $info;
   }

      }
}

$output['parsed'] = $parsed;

    $resultFile  = file_put_contents('sites/default/files/meus_json/myjson.json', json_encode($parsed));

}

function feature_service_access()
{
return TRUE;

As cURL calls don't work I'm

perikut's picture

As cURL calls don't work I'm stucked there and cannot test passing parameters to a module outside Drupal.

some idea? I'm a newbie to Drupal and I'm actively looking for info everywhere, but ANY HELP or hints would be highly appreciated.

thanks in advance

cURL way

ygerasimov's picture

Please check the tests of services. They all are built with cURL requests.

In order to login it is better to to enable user resource action login and do call there to get session id.

http://www.example.com/compan

谢艳's picture

http://www.example.com/company/2?param=paramvalue

function my_mod_services_resources() {
  $info['company']['retrieve']= array(
    'help' => 'get company information by user id',
    'file' => array(
      'type' => 'inc',
      'module' => '_crm_api',
      'name' => '_crm_api.resources'
    ), 
    'callback' => 'company_retrieve',
    'access callback' => '_company_access',
    'access arguments' => array('view'),
    'access arguments append' => TRUE,
    'args' => array(
        array(
            'name' => 'uid',
            'type' => 'int',
            'description' => ' The uid of the user to retrieve.',
            'source' => array('path' => '0'),
            'source' => array('param' => 'uid'),
            'optional' => TRUE,
        ),
       array(
            'name' => 'param',
            'type' => 'string',
            'description' => ' other param.',
            'source' => array('param' => 'param name'),
            'optional' => TRUE,
        ),
    ),
  );
  return $info;
}

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: