Project

General

Profile

« Previous | Next » 

Revision 5602a381

Added by Niels Hoffmann over 15 years ago

implemented POST capability to write data to CDM

View differences:

modules/cdm_dataportal/cdm_api/cdm_api.module
18 18
require_once ('xml2json.php');
19 19
require_once ('uuids.php');
20 20

  
21

  
21 22

  
22 23

  
23 24
/**
......
49 50
function cdm_api_menu($may_cache) {
50 51
  $items = array();
51 52
  if ($may_cache) {
52
    
53
   $items[] = array(
53
    $items[] = array(
54 54
      // usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");
55 55
      'path' => 'cdm_api/proxy',
56 56
      'callback' => 'proxy_content',
57 57
      'access' => true,
58 58
      'type' => MENU_CALLBACK,
59
      );
60
    
59
    );
61 60
  }
62 61
  
63 62
  return $items;
......
307 306
}
308 307

  
309 308

  
310
function proxy_content($url, $theme = null){
309
function proxy_content($url, $theme = null){
310
  
311 311
  $args = func_get_args();
312 312
  
313 313
  $url = array_shift($args);
314 314
  $theme = array_shift($args);
315
  
316
  $request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
317
  
318
  if($request_method == "POST"){
319
    
320
    $parameters = $_POST;
321
    
322
    $post_data = array();
323
    foreach ($parameters as $k=>$v)
324
    {
325
        $post_data[] = "$k=".utf8_encode($v);
326
    }
327
    $post_data = implode(',', $post_data);
328
    
329
    // testing
330
    $data = request_content(urldecode($url), "POST", $post_data);
331
    print $data;
332
    
333
  }else{
334
    // in all other cases perform a simple get request
335
    //TODO reconsider caching logic in this function
336
    if(!$theme){
337
      // print out JSON, the cache cannot be used since it contains objects
338
      $data = request_content(urldecode($url));
339
      print $data;
340
    } else {
341
      $obj = cdm_ws_load(urldecode($url), false);
342
      array_unshift($args, $theme, $obj);
343
      print call_user_func_array('theme', $args);
344
    }
345
  }
315 346
  
316
  //TODO reconsider caching logic in this function
317
  if(!$theme){
318
    // print out JSON, the cache cannot be used since it contains objetcs
319
    $data = get_content(urldecode($url));
320
    print $data;
321
  } else {
322
    $obj = cdm_ws_load(urldecode($url), false);
323
    array_unshift($args, $theme, $obj);
324
    print call_user_func_array('theme', $args);
325
  }
347

  
326 348
}
327 349

  
328 350

  
......
354 376
 *        Relative means relative to the web service base url which is stored in cdm_webservice_url
355 377
 * @return An object or false
356 378
 */
357
function cdm_ws_load($url, $is_multi_paramater_query){
379
function cdm_ws_load($url, $is_multi_paramater_query, $method="GET"){
358 380
  
359
  $do_cache = !$is_multi_paramater_query && variable_get('cdm_webservice_cache', 0);
381
  $do_cache = !$is_multi_paramater_query && variable_get('cdm_webservice_cache', 0) && $method == "GET";
360 382
  $cache_entry = false;
361 383
  
362 384
  // append the default featureTree to url string
......
376 398
    // load fresh data from webservice
377 399
    $time_get_start = microtime(true);
378 400
    // request data from webservice JSON or XML
379
    $datastr = get_content($url);
401
    $datastr = request_content($url, $method);
380 402
    $time_get = microtime(true) - $time_get_start;
381 403
    /*if(TRUE){
382 404
      $storef =  urlencode($url);
......
438 460
  }
439 461
  return $obj;
440 462
}
463

  
464

  
465

  
441 466

  
442
function get_content($url){
467
function request_content($url, $method="GET", $parameters = array()){
443 468
  global $locale;  // drupal variable containing the current locale i.e. language
444 469
  static $header;
445 470
  
......
453 478
  if(function_exists('curl_init')){
454 479

  
455 480
    // use the CURL lib if installed it is supposed to be 20x faster
456
    return _get_content_curl($url, $header);
481
    return _request_content_curl($url, $header, $method, $parameters);
457 482
  } else {
458
    return _get_content_fsockopen($url, $header);
483
    return _request_content_fsockopen($url, $header, $method, $parameters);
459 484
  }
460 485
}
461 486

  
462 487

  
463
function _get_content_fsockopen($url, $header = array()){
488
function _request_content_fsockopen($url, $header = array(), $method = "GET"){
464 489
  //FIXME implement get_content_fsockopen($url);
465
   watchdog('CDM_API', '_get_content_fsockopen - UNIMPLEMENTED', WATCHDOG_ERROR);
490
   watchdog('CDM_API', '_request_content_fsockopen - UNIMPLEMENTED', WATCHDOG_ERROR);
466 491
   return false;
467 492
}
468 493

  
......
475 500
 *
476 501
 * @author Luiz Miguel Axcar (lmaxcar@yahoo.com.br)
477 502
*/
478
function _get_content_curl($url, $header = array())
503
function _request_content_curl($url, $header = array(), $method = "GET", $parameters = array())
479 504
{
480 505
    $ch = curl_init();
481 506

  
......
490 515
    }
491 516
    // set headers
492 517
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
493

  
518
    // set method if not default
519
    if($method != "GET"){
520
      if($method == "POST"){
521
        
522
        curl_setopt ($ch, CURLOPT_POST, 1);
523
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $parameters);
524
        
525
      }else{
526
        // other obscure http methods get passed to curl directly
527
        // TODO generic parameter/body handling
528
        curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
529
      }
530
    }
531
    
532
    
494 533
    ob_start();
495 534
    curl_exec ($ch);
496 535
    if(curl_errno($ch)){
497
      watchdog('CDM_API', '_get_content_curl() - '.curl_error($ch).' URL: '.$url, WATCHDOG_ERROR);
536
      watchdog('CDM_API', '_request_content_curl() - '.curl_error($ch).' REQUEST-METHOD:'.$method.' URL: '.$url, WATCHDOG_ERROR);
498 537
        if(variable_get('cdm_webservice_debug', 1)){
499
          drupal_set_message('_get_content_curl() - '.curl_error($ch).' URL: '.$url, 'error');
538
          drupal_set_message('_request_content_curl() - '.curl_error($ch).' REQUEST-METHOD:'.$method.' URL: '.$url, 'error');
500 539
        }
501 540
    }
502 541
    curl_close ($ch);
......
677 716
 */
678 717
define('CDM_WS_FEATURETREES', 'features/tree');
679 718

  
719

  
720
/**
721
 * Web Service Arguments: {CdmObjUuid}
722
 *
723
 * Either retrieves or writes annotations to any annotatable entity
724
 */
725
define('CDM_WS_ANNOTATIONS', 'annotations');
680 726

  

Also available in: Unified diff