Project

General

Profile

« Previous | Next » 

Revision 2ebf0428

Added by Andreas Kohlbecker over 10 years ago

adding full support for POST requests

View differences:

7.x/modules/cdm_dataportal/cdm_api/cdm_api.module
109 109

  
110 110
      // stip of webservice base url
111 111
      $data['ws_uri'] = str_replace($cdm_ws_url, '', $data['ws_uri']);
112
      if($data['method'] == 'POST'){
113
        $data['ws_uri'] = 'POST: ' . $data['ws_uri'] . '?' . $data['post_data'];
114
      }
112 115

  
113 116
      $cells = array();
114 117
      foreach ($field_map as $field => $label){
......
725 728
 * @see http://drupal.org/node/1354
726 729
 */
727 730
function proxy_content($uri, $hook = NULL) {
731

  
728 732
  $args = func_get_args();
729 733
  $do_gzip = function_exists('gzencode');
730 734
  $uriEncoded = array_shift($args);
731 735
  $uri = urldecode($uriEncoded);
732 736
  $hook = array_shift($args);
737
  $request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
738

  
739
  if ($request_method == "POST" || $request_method == "PUT") {
740
    // read response body via inputstream module
741
    $post_data = file_get_contents("php://input");
742
  }
733 743

  
734 744
  // Find and deserialize arrays.
735 745
  foreach ($args as &$arg) {
736 746
    // FIXME use regex to find serialized arrays.
737
    //       or should we accept json instead pf php serializations?
747
    //       or should we accept json instead of php serializations?
738 748
    if (strpos($arg, "a:") === 0) {
739 749
      $arg = unserialize($arg);
740 750
    }
741 751
  }
742 752

  
743
  $request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
744
  if ($request_method == "POST") {
745
    // this is an experimental feature which will allow to
746
    // write data inot the cdm via the RESTfull web service
747
    $parameters = $_POST;
748
    $post_data = array();
749

  
750
    foreach ($parameters as $k => $v) {
751
      $post_data[] = "$k=" . utf8_encode($v);
752
    }
753
    $post_data = implode(',', $post_data);
753
  // In all these cases perform a simple get request.
754
  // TODO reconsider caching logic in this function.
754 755

  
755
    // For testing.
756
    // TODO use cdm_http_request(..) instead; ( CDM_HTTP_REQUEST_TIMEOUT is already set in there )
757
    $data = drupal_http_request($uri, array('headers' => "POST", 'method' => $post_data, 'timeout' => CDM_HTTP_REQUEST_TIMEOUT));
758
    // print $data;
759
  } else {
760
    // Not a "POST" request
761
    // In all these cases perform a simple get request.
762
    // TODO reconsider caching logic in this function.
763

  
764
    if (empty($hook)) {
765
      // simply return the webservice response
766
      // Print out JSON, the cache cannot be used since it contains objects.
767
      $http_response = cdm_http_request($uri);
768
      if (isset($http_response->headers)) {
769
        foreach ($http_response->headers as $hname => $hvalue) {
770
          drupal_add_http_header($hname, $hvalue);
771
        }
772
      }
773
      if (isset($http_response->data)) {
774
        print $http_response->data;
775
        flush();
776
      }
777
      exit(); // leave drupal here
778
    } else {
779
      // $hook has been supplied
780
      // handle $hook either as compose ot theme hook
781
      // pass through theme or comose hook
782

  
783
      // do a security check since the $uri will be passed
784
      // as absolute URI to cdm_ws_get()
785
      if( !_is_cdm_ws_uri($uri)) {
786
        drupal_set_message(
787
            'Invalid call of proxy_content() with callback parameter \'' . $hook . '\' and URI:' . $uri,
788
            'error'
789
        );
790
        return '';
756
  if (empty($hook)) {
757
    // simply return the webservice response
758
    // Print out JSON, the cache cannot be used since it contains objects.
759
    $http_response = cdm_http_request($uri, $request_method, $post_data);
760
    if (isset($http_response->headers)) {
761
      foreach ($http_response->headers as $hname => $hvalue) {
762
        drupal_add_http_header($hname, $hvalue);
791 763
      }
764
    }
765
    if (isset($http_response->data)) {
766
      print $http_response->data;
767
      flush();
768
    }
769
    exit(); // leave drupal here
770
  } else {
771
    // $hook has been supplied
772
    // handle $hook either as compose ot theme hook
773
    // pass through theme or comose hook
774

  
775
    // do a security check since the $uri will be passed
776
    // as absolute URI to cdm_ws_get()
777
    if( !_is_cdm_ws_uri($uri)) {
778
      drupal_set_message(
779
      'Invalid call of proxy_content() with callback parameter \'' . $hook . '\' and URI:' . $uri,
780
      'error'
781
          );
782
          return '';
783
    }
792 784

  
793
      $obj = cdm_ws_get($uri, NULL, NULL, NULL, TRUE);
785
    $obj = cdm_ws_get($uri, NULL, $post_data, $request_method, TRUE);
794 786

  
795
      $reponse_data = NULL;
787
    $reponse_data = NULL;
796 788

  
797
      if (function_exists('compose_' . $hook)){
798
        // call compose hook
789
    if (function_exists('compose_' . $hook)){
790
      // call compose hook
791

  
792
      $elements =  call_user_func('compose_' . $hook, $obj);
793
      // pass the render array to drupal_render()
794
      $reponse_data = drupal_render($elements);
795
    } else {
796
      // call theme hook
797

  
798
      // TODO use theme registry to get the registered hook info and
799
      //    use these defaults
800
      switch($hook) {
801
        case 'cdm_taxontree':
802
          $variables = array(
803
          'tree' => $obj,
804
          'filterIncludes' => isset($args[0]) ? $args[0] : NULL,
805
          'show_filter_switch' => isset($args[1]) ? $args[1] : FALSE,
806
          'tree_node_callback' => isset($args[2]) ? $args[2] : FALSE,
807
          'element_name'=> isset($args[3]) ? $args[3] : FALSE,
808
          );
809
          $reponse_data = theme($hook, $variables);
810
          break;
811

  
812
        case 'cdm_media_caption':
813
          $variables = array(
814
          'media' => $obj,
815
          // $args[0] is set in taxon_image_gallery_default in
816
          // cdm_dataportal.page.theme.
817
          'elements' => isset($args[0]) ? $args[0] : array(
818
          'title',
819
          'description',
820
          'artist',
821
          'location',
822
          'rights',
823
          ),
824
          'fileUri' => isset($args[1]) ? $args[1] : NULL,
825
          );
826
          $reponse_data = theme($hook, $variables);
827
          break;
828

  
829
        default:
830
          drupal_set_message(t(
831
          'Theme !theme is not supported yet by function !function.', array(
832
          '!theme' => $hook,
833
          '!function' => __FUNCTION__,
834
          )), 'error');
835
          break;
836
      } // END of theme hook switch
837
    } // END of tread as theme hook
838

  
839

  
840
    if($do_gzip){
841
      $reponse_data = gzencode($reponse_data, 2, FORCE_GZIP);
842
      drupal_add_http_header('Content-Encoding', 'gzip');
843
    }
844
    drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
845
    drupal_add_http_header('Content-Length', strlen($reponse_data));
846

  
847
    print $reponse_data;
848
  } // END of handle $hook either as compose ot theme hook
799 849

  
800
        $elements =  call_user_func('compose_' . $hook, $obj);
801
        // pass the render array to drupal_render()
802
        $reponse_data = drupal_render($elements);
803
      } else {
804
        // call theme hook
805

  
806
        // TODO use theme registry to get the registered hook info and
807
        //    use these defaults
808
        switch($hook) {
809
          case 'cdm_taxontree':
810
            $variables = array(
811
              'tree' => $obj,
812
              'filterIncludes' => isset($args[0]) ? $args[0] : NULL,
813
              'show_filter_switch' => isset($args[1]) ? $args[1] : FALSE,
814
              'tree_node_callback' => isset($args[2]) ? $args[2] : FALSE,
815
              'element_name'=> isset($args[3]) ? $args[3] : FALSE,
816
            );
817
            $reponse_data = theme($hook, $variables);
818
            break;
819

  
820
          case 'cdm_media_caption':
821
            $variables = array(
822
              'media' => $obj,
823
              // $args[0] is set in taxon_image_gallery_default in
824
              // cdm_dataportal.page.theme.
825
              'elements' => isset($args[0]) ? $args[0] : array(
826
                'title',
827
                'description',
828
                'artist',
829
                'location',
830
                'rights',
831
              ),
832
              'fileUri' => isset($args[1]) ? $args[1] : NULL,
833
            );
834
            $reponse_data = theme($hook, $variables);
835
            break;
836

  
837
          default:
838
            drupal_set_message(t(
839
            'Theme !theme is not supported yet by function !function.', array(
840
            '!theme' => $hook,
841
            '!function' => __FUNCTION__,
842
            )), 'error');
843
            break;
844
        } // END of theme hook switch
845
      } // END of tread as theme hook
846
      if($do_gzip){
847
        $reponse_data = gzencode($reponse_data, 2, FORCE_GZIP);
848
        drupal_add_http_header('Content-Encoding', 'gzip');
849
      }
850
      drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
851
      drupal_add_http_header('Content-Length', strlen($reponse_data));
852
      print $reponse_data;
853
    } // END of handle $hook either as compose ot theme hook
854
  }
855 850
}
856 851

  
857 852
/**
......
975 970
 * @param int $pageNumber
976 971
 *   The number of the page to be returned, the first page has the
977 972
 *   pageNumber = 0
973
 * @param array $query
974
 *   A array holding the HTTP request query parameters for the request
975
 * @param string $method
976
 *   The HTTP method to use, valid values are "GET" or "POST"
978 977
 * @param bool $absoluteURI
979 978
 *   TRUE when the URL should be treated as absolute URL.
979
 *
980 980
 * @return the a CDM Pager object
981 981
 *
982 982
 */
983
function cdm_ws_page($resourceURI, $pageSize, $pageNumber,  $absoluteURI = FALSE) {
984
  return cdm_ws_get($resourceURI, NULL, queryString(array(
985
      "pageNumber" => $pageNumber,
986
      'pageSize' => $pageSize,
987
    )),
988
    'GET',
989
    $absoluteURI);
983
function cdm_ws_page($resourceURI, $pageSize, $pageNumber, array $query = array(), $method = 'GET', $absoluteURI = FALSE) {
984

  
985
  $query['pageNumber'] = $pageNumber;
986
  $query['pageSize'] = $pageSize;
987

  
988
  return cdm_ws_get($resourceURI, NULL, queryString($query), $method, $absoluteURI);
990 989
}
991 990

  
992 991
/**
993 992
 * Fetches all entities from the given REST endpoint using the pager mechanism.
994 993
 *
995 994
 * @param string $resourceURI
995
 * @param array $query
996
 *   A array holding the HTTP request query parameters for the request
997
 * @param string $method
998
 *   The HTTP method to use, valid values are "GET" or "POST";
996 999
 * @param bool $absoluteURI
997 1000
 *   TRUE when the URL should be treated as absolute URL.
998 1001
 *
......
1000 1003
 *     A list of CDM entitites
1001 1004
 *
1002 1005
 */
1003
function cdm_ws_fetch_all($resourceURI, $absoluteURI = FALSE) {
1006
function cdm_ws_fetch_all($resourceURI, array $query = array(), $method = 'GET', $absoluteURI = FALSE) {
1004 1007
  $page_index = 0;
1005 1008
  // using a bigger page size to avoid to many multiple requests
1006 1009
  $page_size = 500;
1007 1010
  $entities = array();
1008 1011

  
1009 1012
  while ($page_index !== FALSE){
1010
    $pager =  cdm_ws_page($resourceURI, $page_size, $page_index, $absoluteURI);
1013
    $pager =  cdm_ws_page($resourceURI, $page_size, $page_index, $query,  $method, $absoluteURI);
1011 1014
    if(isset($pager->records) && is_array($pager->records)) {
1012 1015
      $entities = $pager->records;
1013 1016
      if(!empty($pager->nextIndex)){
......
1093 1096
    $response = NULL;
1094 1097

  
1095 1098
    // 1st try
1096
    $response = cdm_ws_get(cdm_compose_taxonomy_root_level_path($taxon_uuid), NULL, NULL, NULL, TRUE);
1099
    $response = cdm_ws_get(cdm_compose_taxonomy_root_level_path($taxon_uuid), NULL, NULL, 'GET', TRUE);
1097 1100

  
1098 1101
    if ($response == NULL) {
1099 1102
      // 2dn try by ignoring the rank limit
1100
      $response = cdm_ws_get(cdm_compose_taxonomy_root_level_path($taxon_uuid, TRUE), NULL, NULL, NULL, TRUE);
1103
      $response = cdm_ws_get(cdm_compose_taxonomy_root_level_path($taxon_uuid, TRUE), NULL, NULL, 'GET', TRUE);
1101 1104
    }
1102 1105

  
1103 1106
    if ($response == NULL) {
......
1110 1113
      }
1111 1114
      else {
1112 1115
        // Check if taxonomictree_uuid is valid.
1113
        $test = cdm_ws_get(cdm_compose_taxonomy_root_level_path(), NULL, NULL, NULL, TRUE);
1116
        $test = cdm_ws_get(cdm_compose_taxonomy_root_level_path(), NULL, NULL, 'GET', TRUE);
1114 1117
        if ($test == NULL) {
1115 1118
          // The default set by the admin seems to be invalid or is not even set.
1116 1119
          drupal_set_message(_no_classfication_uuid_message(), 'warning');
......
1156 1159
    }
1157 1160
    else {
1158 1161
      // Check if taxonomictree_uuid is valid.
1159
      $test = cdm_ws_get(cdm_compose_taxonomy_root_level_path(), NULL, NULL, NULL, TRUE);
1162
      $test = cdm_ws_get(cdm_compose_taxonomy_root_level_path(), NULL, NULL, 'GET', TRUE);
1160 1163
      if ($test == NULL) {
1161 1164
        // The default set by the admin seems to be invalid or is not even set.
1162 1165
        drupal_set_message(_no_classfication_uuid_message(), 'warning');
......
1295 1298

  
1296 1299
  $merged_trees = array();
1297 1300

  
1298
  $description_elemens = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON . '?' . queryString(
1301
  $description_elemens = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1299 1302
      array(
1300 1303
      'taxon' => $taxon_uuid,
1301 1304
      'features' => cdm_featureTree_elements_toString($feature_tree->root, ',', 'uuid')
1302
      )
1303
    )
1305
      ),
1306
      'POST'
1304 1307
  );
1305 1308

  
1306 1309
  // Combine all descripions into one feature tree.
......
1447 1450
}
1448 1451

  
1449 1452
/**
1450
 * Sends a GET request to a CDM RESTService and returns a deserialized object.
1453
 * Sends a GET or POST request to a CDM RESTService and returns a deserialized object.
1451 1454
 *
1452 1455
 * The response from the HTTP GET request is returned as object.
1453 1456
 * The response objects coming from the webservice configured in the
......
1484 1487

  
1485 1488
  static $cacheL1 = array();
1486 1489

  
1490
  $data = NULL;
1491
  // store query string in $data and clear the query, $data will be set as HTTP request body
1492
  if($method == 'POST'){
1493
    $data = $query;
1494
    $query = NULL;
1495
  }
1496

  
1487 1497
  // Transform the given uri path or pattern into a proper webservice uri.
1488 1498
  if (!$absoluteURI) {
1489 1499
    $uri = cdm_compose_url($uri, $pathParameters, $query);
......
1492 1502
  $is_cdm_ws_uri = _is_cdm_ws_uri($uri);
1493 1503
  $use_cacheL2 = variable_get('cdm_webservice_cache', 1);
1494 1504

  
1495
  if (array_key_exists($uri, $cacheL1)) {
1505
  if($method == 'GET'){
1506
    $cache_key = $uri;
1507
  } else {
1508
    // sha1 creates longer hashes and thus will cause fewer collisions than md5.
1509
    // crc32 is faster but creates much shorter hashes
1510
    $cache_key = $uri . '[' . $method . ':' . sha1($data) .']';
1511
  }
1512

  
1513
  if (array_key_exists($cache_key, $cacheL1)) {
1496 1514
    $cacheL1_obj = $cacheL1[$uri];
1497 1515
  }
1498 1516

  
......
1507 1525

  
1508 1526
  if ($use_cacheL2) {
1509 1527
    // Try to get object from cacheL2.
1510
    $cacheL2_entry = cache_get($uri, 'cache_cdm_ws');
1528
    $cacheL2_entry = cache_get($cache_key, 'cache_cdm_ws');
1511 1529
  }
1512 1530

  
1513 1531
  if (isset($cacheL1_obj)) {
......
1516 1534
    //
1517 1535
    $obj = $cacheL1_obj;
1518 1536
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1519
      cdm_ws_debug_add($uri, 0, 0, NULL, 'cacheL1');
1537
      cdm_ws_debug_add($uri, $method, $data, 0, 0, NULL, 'cacheL1');
1520 1538
    }
1521 1539
  }
1522 1540
  elseif ($cacheL2_entry) {
......
1528 1546
    $duration_parse = microtime(TRUE) - $duration_parse_start;
1529 1547

  
1530 1548
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1531
      cdm_ws_debug_add($uri, 0, $duration_parse, NULL, 'cacheL2');
1549
      cdm_ws_debug_add($uri, $method, $data, 0, $duration_parse, NULL, 'cacheL2');
1532 1550
    }
1533 1551
  }
1534 1552
  else {
......
1537 1555
    //
1538 1556
    $duration_fetch_start = microtime(TRUE);
1539 1557
    // Request data from webservice JSON or XML.
1540
    $response = cdm_http_request($uri, $method);
1541
    $datastr = NULL;
1558
    $response = cdm_http_request($uri, $method, $data);
1559
    $response_body = NULL;
1542 1560
    if (isset($response->data)) {
1543
      $datastr = $response->data;
1561
      $response_body = $response->data;
1544 1562
    }
1545 1563
    $duration_fetch = microtime(TRUE) - $duration_fetch_start;
1546 1564
    $duration_parse_start = microtime(TRUE);
1547 1565

  
1548 1566
    // Parse data and create object.
1549
    $obj = cdm_load_obj($datastr);
1567
    $obj = cdm_load_obj($response_body);
1550 1568

  
1551 1569
    $duration_parse = microtime(TRUE) - $duration_parse_start;
1552 1570
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1553
      if ($obj || $datastr == "[]") {
1571
      if ($obj || $response_body == "[]") {
1554 1572
        $status = 'valid';
1555 1573
      }
1556 1574
      else {
1557 1575
        $status = 'invalid';
1558 1576
      }
1559
      cdm_ws_debug_add($uri, $duration_fetch, $duration_parse, strlen($datastr), $status);
1577
      cdm_ws_debug_add($uri, $method, $data, $duration_fetch, $duration_parse, strlen($response_body), $status);
1560 1578
    }
1561 1579
    if ($set_cacheL2) {
1562 1580
      // Store the object in cache L2.
1563
      // Comment @WA perhaps better if Drupal serializes here? Then the
1581
      // Comment @WA perhaps better if Drupal serializedatas here? Then the
1564 1582
      // flag serialized is set properly in the cache table.
1565
      cache_set($uri, serialize($obj), 'cache_cdm_ws', CACHE_TEMPORARY);
1583
      cache_set($cache_key, serialize($obj), 'cache_cdm_ws', CACHE_TEMPORARY);
1566 1584
    }
1567 1585
  }
1568 1586
  if ($obj) {
1569 1587
    // Store the object in cache L1.
1570 1588
    if ($set_cacheL1) {
1571
      $cacheL1[$uri] = $obj;
1589
      $cacheL1[$cache_key] = $obj;
1572 1590
    }
1573 1591
  }
1574 1592
  return $obj;
......
1581 1599
 *
1582 1600
 * @param $uri
1583 1601
 *    The CDM REST URI to which the request has been send
1602
 * @param string $method
1603
 *    The HTTP request method, either 'GET' or 'POST'
1604
 * @param string $post_data
1605
 *    The datastring send with a post request
1584 1606
 * @param $duration_fetch
1585 1607
 *    The time in seconds it took to fetch the data from the web service
1586 1608
 * @param $duration_parse
......
1592 1614
 * @return bool
1593 1615
 *    TRUE if adding the debug information was successful
1594 1616
 */
1595
function cdm_ws_debug_add($uri, $duration_fetch, $duration_parse, $datasize, $status) {
1617
function cdm_ws_debug_add($uri, $method, $post_data, $duration_fetch, $duration_parse, $datasize, $status) {
1596 1618

  
1597 1619
  static $initial_time = NULL;
1598 1620
  if(!$initial_time) {
......
1621 1643
  // data links to make data accecsible as json and xml
1622 1644
  $data_links = '';
1623 1645
  if (_is_cdm_ws_uri($path)) {
1624
    $data_links .= '<a href="' . $xml_uri . '" target="data">xml</a>-';
1625
    $data_links .= '<a href="' . url('cdm_api/proxy/' . urlencode($xml_uri)) . '" target="data">proxied</a>';
1626
    $data_links .= '<br/>';
1627
    $data_links .= '<a href="' . $json_uri . '" target="data">json</a>-';
1628
    $data_links .= '<a href="' . url('cdm_api/proxy/' . urlencode($json_uri)) . '" target="data">proxied</a>';
1646

  
1647
    // see ./js/http-method-link.js
1648

  
1649
    if($method == 'GET'){
1650
      $data_links .= '<a href="' . $xml_uri . '" target="data">xml</a>-';
1651
      $data_links .= '<a href="' . url('cdm_api/proxy/' . urlencode($xml_uri)) . '" target="data">proxied</a>';
1652
      $data_links .= '<br/>';
1653
      $data_links .= '<a href="' . $json_uri . '" target="data">json</a>-';
1654
      $data_links .= '<a href="' . url('cdm_api/proxy/' . urlencode($json_uri)) . '" target="data">proxied</a>';
1655
    } else {
1656
      $js_link_activation = 'class="http-' . $method . '-link" data-cdm-http-post="' . $post_data . '" type="application/x-www-form-urlencoded"';
1657
      $data_links .= '<a ' . $js_link_activation . ' href="' . url('cdm_api/proxy/' . urlencode($xml_uri)) . '" target="data">xml-proxied</a>';
1658
      $data_links .= '<br/>';
1659
      $data_links .= '<a ' . $js_link_activation . ' href="' . url('cdm_api/proxy/' . urlencode($json_uri)) . '" target="data">json-proxied</a>';
1660
    }
1629 1661
  }
1630 1662
  else {
1631 1663
    $data_links .= '<a href="' . $uri . '" target="data">open</a>';
......
1634 1666
  //
1635 1667
  $data = array(
1636 1668
      'ws_uri' => $uri,
1669
      'method' => $method,
1670
      'post_data' => $post_data,
1637 1671
      'time' => sprintf('%3.3f', $time),
1638 1672
      'fetch_seconds' => sprintf('%3.3f', $duration_fetch),
1639 1673
      'parse_seconds' => sprintf('%3.3f', $duration_parse),
......
1658 1692
 * @todo Please document this function.
1659 1693
 * @see http://drupal.org/node/1354
1660 1694
 */
1661
function cdm_load_obj($datastr) {
1662
  $obj = json_decode($datastr);
1695
function cdm_load_obj($response_body) {
1696
  $obj = json_decode($response_body);
1663 1697

  
1664 1698
  if (!(is_object($obj) || is_array($obj))) {
1665 1699
    ob_start();
......
1679 1713
 * @param string $method
1680 1714
 *   The HTTP method to use, valid values are "GET" or "POST"; defaults to
1681 1715
 *   "GET" even if NULL, FALSE or any invalid value is supplied.
1682
 * @param array $parameters
1683
 *   Parameters to use in the request.
1684
 * @param array $header
1685
 *   The header to include in the request.
1716
 * @param $data: A string containing the request body, formatted as
1717
 *     'param=value&param=value&...'. Defaults to NULL.
1686 1718
 *
1687 1719
 * @return object
1688 1720
 *   The object as returned by drupal_http_request():
......
1703 1735
 *     easy access the array keys are returned in lower case.
1704 1736
 *   - data: A string containing the response body that was received.
1705 1737
 */
1706
function cdm_http_request($uri, $method = "GET", $parameters = array(), $header = array(), $options = NULL) {
1738
function cdm_http_request($uri, $method = "GET", $data = NULL) {
1707 1739
  static $acceptLanguage = NULL;
1708 1740

  
1709 1741
  if (!$acceptLanguage) {
......
1720 1752
  }
1721 1753

  
1722 1754
  if ($method != "GET" && $method != "POST") {
1723
    $method = "GET";
1755
    drupal_set_message('cdm_api.module#cdm_http_request() : unsupported HTTP request method ', 'error');
1724 1756
  }
1725 1757

  
1726 1758
  if (empty($header) && _is_cdm_ws_uri($uri)) {
......
1729 1761
    $header['Accept-Charset'] = 'UTF-8';
1730 1762
  }
1731 1763

  
1764
  if($method == "POST") {
1765
    // content type is application/x-www-form-urlencoded, so the request body uses the same format as the query string
1766
    $header['Content-Type'] = 'application/x-www-form-urlencoded';
1767
  }
1768

  
1732 1769
  return drupal_http_request($uri, array(
1733 1770
      'headers' => $header,
1734 1771
      'method' => $method,
1772
      'data' => $data,
1735 1773
      'timeout' => CDM_HTTP_REQUEST_TIMEOUT
1736 1774
      )
1737 1775
   );
......
1757 1795
  foreach ($root_node->children as $feature_node) {
1758 1796
    $out .= ($out ? $separator : '');
1759 1797
    $out .= $feature_node->feature->$field_name;
1760
    if (is_array($feature_node->children)) {
1761
      $childlabels = '';
1762
      foreach ($feature_node->children as $child_node) {
1763
        $childlabels .= ($childlabels ? $separator : '');
1764
        $childlabels .= cdm_featureTree_elements_toString($child_node, $separator, $field_name);
1765
      }
1798
    if (is_array($feature_node->children) && count($feature_node->children) > 0) {
1799
      $childlabels = cdm_featureTree_elements_toString($feature_node, $separator, $field_name);
1766 1800
      if (strlen($childlabels)) {
1767
        $out .=  $pre_child_separator . $childlabels . $post_child_separator;
1801
          $out .=  $pre_child_separator . $childlabels . $post_child_separator;
1768 1802
      }
1769 1803
    }
1770 1804
  }
......
2137 2171
      'weight' => JS_LIBRARY,
2138 2172
      'cache' => TRUE)
2139 2173
    );
2174
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/http-method-link.js',
2175
    array(
2176
    'type' => 'file',
2177
    'weight' => JS_LIBRARY,
2178
    'cache' => TRUE)
2179
    );
2140 2180

  
2141 2181
}
2142 2182

  

Also available in: Unified diff