View file lib/curl.php

File size: 3.92Kb
<?
/**
 * Автор Орёл
 */
class CurlManager {

        static public $display_errors = 0;

        static public function curl_query($options, $url = 0) {
                if ($url){
                        $ch = curl_init($url);
                } else {
                        $ch = curl_init();
                }

                if (!empty($options)) {
                        curl_setopt_array($ch, $options);
                }

                $result = curl_exec($ch);

                if (self :: $display_errors) {
                        if (!curl_errno($ch)) {
                                $info = curl_getinfo($ch);
                                die('Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']);
                        } else {
                                die('Curl error: ' . curl_error($ch));
                        }
                }

                curl_close($ch);
                return $result;
        }
        /**
         * Мульти curl без опций
         * @param <type> $url
         * @return <type>
         */
        static public function curl_multiquery_get($url) {
                return self :: $this->multicurl_request($url);
        }
        /**
         * Мульти curl с опциями и возвратом результата
         * @param <type> $options
         * @return <type>
         */
        static public function curl_multiquery_fetch($options) {
                return self :: multicurl_request_opt($options);
        }

        /**
         * PHP.NET Multi Curl
         * @param <type> $nodes
         * @return <type>
         */
        private function multicurl_request($nodes) {
                $mh = curl_multi_init();
                $curl_array = array();
                foreach ($nodes as $i => $url) {
                        $curl_array[$i] = curl_init($url);
                        curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
                        curl_multi_add_handle($mh, $curl_array[$i]);
                }
                $running = NULL;
                do {
                        curl_multi_exec($mh, $running);
                } while ($running > 0);

                $res = array();
                foreach ($nodes as $i => $url) {
                        $res[$url] = curl_multi_getcontent($curl_array[$i]);
                }

                foreach ($nodes as $i => $url) {
                        curl_multi_remove_handle($mh, $curl_array[$i]);
                }
                curl_multi_close($mh);
                return $res;
        }

        /**
         * PHP.NET Multi Curl
         * Mode options
         * @author mode: Orel
         * @param <type> $nodes
         * @param <type> $options
         * @return <type>
         */
        private function multicurl_request_opt($options) {
                $mh = curl_multi_init();
                $curl_array = array();
                
                foreach ($options as $i => $option_data) {
                        $curl_array[$i] = curl_init($url);
                        foreach ($option_data as $opt => $val){
                                curl_setopt($curl_array[$i], $opt, $val);
                        }
                        curl_multi_add_handle($mh, $curl_array[$i]);
                }
                $running = NULL;
                do {
                        curl_multi_exec($mh, $running);
                } while ($running > 0);

                $res = array();
                foreach ($options as $i => $data) {
                        $res[$i] = curl_multi_getcontent($curl_array[$i]);
                }

                foreach ($options as $i => $data) {
                        curl_multi_remove_handle($mh, $curl_array[$i]);
                }
                curl_multi_close($mh);
                return $res;
        }

}
?>