Project

General

Profile

« Previous | Next » 

Revision 3db2866c

Added by Andreas Kohlbecker almost 11 years ago

removing curl based http request support - we now rely only on the drupal built in function drupal_http_request()

View differences:

7.x/modules/cdm_dataportal/cdm_api/cdm_api.module
1565 1565
  }
1566 1566

  
1567 1567
  if (empty($header) && _is_cdm_ws_uri($uri)) {
1568
    $header['Accept'] = (variable_get('cdm_webservice_type', 'json') == 'json' ? 'application/json' : 'text/xml');
1568
    $header['Accept'] = 'application/json';
1569 1569
    $header['Accept-Language'] = $acceptLanguage;
1570 1570
    $header['Accept-Charset'] = 'UTF-8';
1571 1571
  }
1572 1572

  
1573
  if (FALSE && function_exists('curl_init')) {
1574
    // !!!!! CURL Disabled due to problems with following redirects
1575
    // (CURLOPT_FOLLOWLOCATION=1) and safe_mode = on
1576
    // Use the CURL lib if installed, it is supposed to be 20x faster.
1577
    return _http_request_using_curl($uri, $header, $method, $parameters);
1578
  }
1579
  else {
1580
    return _http_request_using_fsockopen($uri, $header, $method, $parameters);
1581
  }
1582
}
1583

  
1584
/**
1585
 * @todo Please document this function.
1586
 * @see http://drupal.org/node/1354
1587
 */
1588
function _http_request_using_fsockopen($uri, $header = array(), $method = 'GET') {
1589 1573
  $response = drupal_http_request($uri, array('headers' => $header, 'method' => $method, 'timeout' => CDM_HTTP_REQUEST_TIMEOUT));
1590 1574
  if (isset($response->data)) {
1591 1575
      return $response->data;
1592 1576
  }
1593 1577
}
1594 1578

  
1595
/**
1596
 * Return string content from a remote file.
1597
 *
1598
 * @author Luiz Miguel Axcar <lmaxcar@yahoo.com.br>
1599
 *
1600
 * @param string $uri
1601
 *   The url for which to return content.
1602
 *
1603
 * @return string
1604
 *   The returned content.
1605
 */
1606
function _http_request_using_curl($uri, $headers = array(), $method = "GET", $parameters = array()) {
1607
  $ch = curl_init();
1608

  
1609
  curl_setopt($ch, CURLOPT_URL, $uri);
1610
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
1611
  curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
1612

  
1613
  // Set proxy settings.
1614
  if (variable_get('cdm_webservice_proxy_url', FALSE)) {
1615
    curl_setopt($ch, CURLOPT_PROXY, variable_get('cdm_webservice_proxy_url', ''));
1616
    curl_setopt($ch, CURLOPT_PROXYPORT, variable_get('cdm_webservice_proxy_port', '80'));
1617
    if (variable_get('cdm_webservice_proxy_usr', FALSE)) {
1618
      curl_setopt($ch, CURLOPT_PROXYUSERPWD, variable_get('cdm_webservice_proxy_usr', '') . ':' . variable_get('cdm_webservice_proxy_pwd', ''));
1619
    }
1620
  }
1621

  
1622
  // Modify headers array to be used by curl.
1623
  foreach ($headers as $header_key => $header_val) {
1624
    $curl_headers[] = $header_key . ': ' . $header_val;
1625
  }
1626
  if (isset($curl_headers)) {
1627
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
1628
  }
1629

  
1630
  // Set method if not default.
1631
  if ($method != "GET") {
1632
    if ($method == "POST") {
1633

  
1634
      curl_setopt($ch, CURLOPT_POST, 1);
1635
      curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
1636
    }
1637
    else {
1638
      // Other obscure http methods get passed to curl directly.
1639
      // TODO generic parameter/body handling
1640
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
1641
    }
1642
  }
1643

  
1644
  ob_start();
1645
  curl_exec($ch);
1646
  $info = curl_getinfo($ch);
1647
  if (curl_errno($ch)) {
1648
    watchdog('CDM_API', '_http_request_curl() - ' . curl_error($ch) . '; REQUEST-METHOD:' . $method . ' URL: ' . substr($uri . ' ', 0, 150), WATCHDOG_ERROR);
1649
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1650
      drupal_set_message(check_plain(t('_http_request_curl() - !curl_error; REQUEST-METHOD:!method URL: !url', array(
1651
      '!curl_error' => curl_error($ch),
1652
      '!method' => $method,
1653
      '!url' => substr($uri . ' ', 0, 150),
1654
       ))), 'error');
1655
    }
1656
  }
1657
  curl_close($ch);
1658
  $string = ob_get_contents();
1659
  ob_end_clean();
1660

  
1661
  return $string;
1662
}
1663

  
1664 1579
/**
1665 1580
 * @todo Please document this function.
1666 1581
 * @see http://drupal.org/node/1354

Also available in: Unified diff