start.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. function url_getter_init() {
  3. if (!function_exists('curl_version')) {
  4. // see, this is why I want a dependency check callback in the core!!
  5. register_error('The URL Getter depends on CURL, but the library was not found. Please install it.');
  6. }
  7. }
  8. /**
  9. * $url: the URL to get
  10. * $args: array of other optional arguments as follows:
  11. * username: username for HTTP auth
  12. * password: password for HTTP auth
  13. * post: boolean, is this a post? (defaults to False)
  14. * data: HTTP body for POST
  15. * headers: HTTP headers
  16. */
  17. function url_getter_getUrl($url, $args = array()) {
  18. global $release;
  19. $userAgent = '(Elgg ' . $release . ')';
  20. $ch = curl_init();
  21. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  22. curl_setopt($ch, CURLOPT_URL, $url);
  23. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  24. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  25. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  28. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  29. if ($args['username'] && $args['password']) {
  30. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  31. curl_setopt($ch, CURLOPT_USERPWD, $args['username'] . ':' . $args['password']);
  32. }
  33. if ($args['post']) {
  34. curl_setopt($ch, CURLOPT_POST, 1);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, $args['data']);
  36. }
  37. if ($args['headers']) {
  38. curl_setopt($ch, CURLOPT_HTTPHEADER, $args['headers']);
  39. }
  40. $html = curl_exec($ch);
  41. $rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  42. //print '[' . $url . ':' . $html . ':' . $rc . ']' . "\n";
  43. if (!$html && $rc != 200) {
  44. return $rc;
  45. } else {
  46. return $html;
  47. }
  48. }
  49. /**
  50. * Fetch a URL and parse it as an XML document.
  51. * Same arguments as getUrl
  52. */
  53. function url_getter_getDoc($url, $args = array()) {
  54. $doc = new DOMDocument();
  55. $xml = url_getter_getUrl($url, $args);
  56. if ($xml && is_string($xml)) {
  57. $xml = trim($xml); // trim out newlines and other whitespace that might confuse the parser
  58. $doc->loadXML($xml);
  59. }
  60. return $doc;
  61. }
  62. elgg_register_event_handler('init','system','url_getter_init');