123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- function url_getter_init() {
- if (!function_exists('curl_version')) {
-
- register_error('The URL Getter depends on CURL, but the library was not found. Please install it.');
- }
- }
- function url_getter_getUrl($url, $args = array()) {
- global $release;
- $userAgent = '(Elgg ' . $release . ')';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_FAILONERROR, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_AUTOREFERER, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt($ch, CURLOPT_VERBOSE, 1);
- if ($args['username'] && $args['password']) {
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($ch, CURLOPT_USERPWD, $args['username'] . ':' . $args['password']);
- }
- if ($args['post']) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $args['data']);
- }
- if ($args['headers']) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $args['headers']);
- }
- $html = curl_exec($ch);
- $rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- if (!$html && $rc != 200) {
- return $rc;
- } else {
- return $html;
- }
- }
- function url_getter_getDoc($url, $args = array()) {
- $doc = new DOMDocument();
- $xml = url_getter_getUrl($url, $args);
- if ($xml && is_string($xml)) {
- $xml = trim($xml);
- $doc->loadXML($xml);
- }
- return $doc;
- }
- elgg_register_event_handler('init','system','url_getter_init');
|