php, curl небольшой скрипт.

Модератор: Модераторы разделов

ford1813
Сообщения: 383
ОС: Slackware

php, curl небольшой скрипт.

Сообщение ford1813 »

Всем добрый день.


Есть ссылка вида https://my.alloincognito.ru/clients/?sc=all...p;password=1111

Если ввести ее в адресной строке броузера , то я смогу спокойно авторизоваться под тестовым номером и паролем.

Как бы ее заставить отрабатывать через консольный curl ?
Контакты для связи:

E-mail: ford1813@gmail.com

Telegram: @ford1813
Спасибо сказали:
Аватара пользователя
drBatty
Сообщения: 8735
Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
ОС: Slackware-current

Re: php, curl небольшой скрипт.

Сообщение drBatty »

попробуйте
lynx
он скрипты умеет обрабатывать
http://emulek.blogspot.ru/ Windows Must Die
Учебник по sed зеркало в github

Скоро придёт
Осень
Спасибо сказали:
ford1813
Сообщения: 383
ОС: Slackware

Re: php, curl небольшой скрипт.

Сообщение ford1813 »

lynx пробовал. Умеет он обрабатывать скрипты или нет , пока оставим в стороне.


Я просто не могу понять почему curl не авторизовывается на сайте , с подсунутыми в него параметрами useragent.

А в том же самом firefox данная строчка нормально отрабатывает.
Контакты для связи:

E-mail: ford1813@gmail.com

Telegram: @ford1813
Спасибо сказали:
Аватара пользователя
drBatty
Сообщения: 8735
Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
ОС: Slackware-current

Re: php, curl небольшой скрипт.

Сообщение drBatty »

wertik писал(а): ↑
06.12.2009 13:57
lynx пробовал. Умеет он обрабатывать скрипты или нет , пока оставим в стороне.

умеет. я пробовал.
а вот curl не юзал, но думаю ему надо подсунуть куку.
вот что в мане написано:
-b/--cookie <name=data>
(HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously
received from the server in a "Set-Cookie:" line. The data should be in the format
"NAME1=VALUE1; NAME2=VALUE2".

If no '=' letter is used in the line, it is treated as a filename to use to read previously
stored cookie lines from, which should be used in this session if they match. Using this method
also activates the "cookie parser" which will make curl record incoming cookies too, which may be
handy if you're using this in combination with the -L/--location option. The file format of the
file to read cookies from should be plain HTTP headers or the Netscape/Mozilla cookie file for-
mat.

NOTE that the file specified with -b/--cookie is only used as input. No cookies will be stored in
the file. To store cookies, use the -c/--cookie-jar option or you could even save the HTTP head-
ers to a file using -D/--dump-header!

If this option is set more than once, the last one will be the one that's used.
http://emulek.blogspot.ru/ Windows Must Die
Учебник по sed зеркало в github

Скоро придёт
Осень
Спасибо сказали:
ford1813
Сообщения: 383
ОС: Slackware

Re: php, curl небольшой скрипт.

Сообщение ford1813 »

Насколько я понимаю подсовывать куку нужно лишь если я буду ходить глубже на 1 ссылку?


куку я уже подсовывал , результата нет.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_URL, "https://my.alloincognito.ru/clients/?sc=allo&action=,go&interface=AUTO&prefix=495&login=8888888&password=1111");
curl_setopt($ch, CURLOPT_URL, "https://my.alloincognito.ru/clients/skel.cgi?sc=allo&action=detal");
echo curl_exec($ch);
curl_close($ch);
?>
Контакты для связи:

E-mail: ford1813@gmail.com

Telegram: @ford1813
Спасибо сказали:
pcodr
Сообщения: 283
ОС: Debian

Re: php, curl небольшой скрипт.

Сообщение pcodr »

Немного личшнего, но весьма универсально.

Код: Выделить всё

<?php
    include("WWW.class.php");

    $cookies = tempnam("/tmp", "cook_");
    $www = new WWW(array('cookies'=>$cookies));

    try
    {
        $page = $www->get("https://my.alloincognito.ru/clients/?sc=allo&action=,go&interface=AUTO&prefix=495&login=8888888&password=1111");
    }
    catch(WWWException $e)
    {
        die($e->getMessage());
    }

    unlink($cookies);
?>


WWW.class.php

Код: Выделить всё

<?php
    class WWW
    {
        private $_ch;

        function __construct($opts=array())
        {
            $this->_ch = curl_init();
            curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            $this->ua($opts['ua'] ? $opts['ua'] : UserAgent::random());
            $this->redirect($opts['redirect'] ? $opts['redirect'] : true);
            $this->timeout($opts['timeout'] ? $opts['timeout'] : 60);
            if($opts['cookies']) $this->cookies($opts['cookies']);
            if($opts['proxy'])   $this->proxy($opts['proxy']);
            if($opts['ip'])      $this->bind($opts['ip']);
        }

        function __destruct()
        {
            curl_close($this->_ch);
        }

        public function ua($ua)
        {
            return curl_setopt($this->_ch, CURLOPT_USERAGENT, $ua);
        }

        public function timeout($timeout)
        {
            return curl_setopt($this->_ch, CURLOPT_TIMEOUT, $timeout);
        }

        public function cookies($cookies)
        {
            return (curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $cookies) && curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $cookies));
        }

        public function redirect($redirectable)
        {
            return curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, $redirectable);
        }

        public function proxy($proxy)
        {
            return curl_setopt($this->_ch, CURLOPT_PROXY, $proxy);
        }

        public function bind($ip)
        {
            return curl_setopt($this->_ch, CURLOPT_INTERFACE, $ip);
        }

        public function referer($referer)
        {
            return curl_setopt($this->_ch, CURLOPT_REFERER, $referer);
        }

        public function get($url, $headers = false, $file = false, $spec_headers = false)
        {
            curl_setopt($this->_ch, CURLOPT_URL, $url);
            curl_setopt($this->_ch, CURLOPT_HEADER, $headers);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $spec_headers);
            }

            if($file)
            {
                if(!$fh = fopen($file, "w"))
                {
                    throw new WWWException("`$file': couldn't open for write");
                }
                curl_setopt($this->_ch, CURLOPT_FILE, $fh);
            }

            $page = curl_exec($this->_ch);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, array());
            }

            if($file)
            {
                fclose($fh);
                curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            }

            if($page === false)
            {
                throw new WWWException(curl_error($this->_ch));
            }

            return $page;
        }

        public function post($url, $post, $headers = false, $file = false, $spec_headers = false)
        {
            curl_setopt($this->_ch, CURLOPT_URL, $url);
            curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($this->_ch, CURLOPT_HEADER, $headers);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $spec_headers);
            }

            if($file)
            {
                if(!$fh = fopen($file, "w"))
                {
                    throw new WWWException("`$file': couldn't open for write");
                }
                curl_setopt($this->_ch, CURLOPT_FILE, $fh);
            }

            $page = curl_exec($this->_ch);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, array());
            }

            if($file)
            {
                fclose($fh);
                curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            }

            if($page === false)
            {
                throw new WWWException(curl_error($this->_ch));
            }

            return $page;
        }
    }

    class WWWException extends Exception { }

    class UserAgent
    {
        static function random()
        {
            $agents = array
            (
                'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
                'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915',
                'Opera/9.63 (Windows NT 6.1; U; de) Presto/2.1.1',
                'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.3) Gecko/20070310 Iceweasel/2.0.0.3',
                'Opera/9.63 (Macintosh; Intel Mac OS X; U; de) Presto/2.1.1',
                'Opera/9.63 (X11; Linux i686; U; de) Presto/2.1.1',
                'Opera/9.50 (X11; Linux ppc; U; en)',
                'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.2)',
                'Mozilla/4.0 (Mozilla/4.0; MSIE 7.0; Windows NT 5.1; FDM; SV1)'
            );

            return $agents[array_rand($agents)];
        }
    }
?>
remote system type is unix
Спасибо сказали:
ford1813
Сообщения: 383
ОС: Slackware

Re: php, curl небольшой скрипт.

Сообщение ford1813 »

pcodr писал(а): ↑
06.12.2009 18:31
Немного личшнего, но весьма универсально.

Код: Выделить всё

<?php
    include("WWW.class.php");

    $cookies = tempnam("/tmp", "cook_");
    $www = new WWW(array('cookies'=>$cookies));

    try
    {
        $page = $www->get("https://my.alloincognito.ru/clients/?sc=allo&action=,go&interface=AUTO&prefix=495&login=8888888&password=1111");
    }
    catch(WWWException $e)
    {
        die($e->getMessage());
    }

    unlink($cookies);
?>


WWW.class.php

Код: Выделить всё

<?php
    class WWW
    {
        private $_ch;

        function __construct($opts=array())
        {
            $this->_ch = curl_init();
            curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            $this->ua($opts['ua'] ? $opts['ua'] : UserAgent::random());
            $this->redirect($opts['redirect'] ? $opts['redirect'] : true);
            $this->timeout($opts['timeout'] ? $opts['timeout'] : 60);
            if($opts['cookies']) $this->cookies($opts['cookies']);
            if($opts['proxy'])   $this->proxy($opts['proxy']);
            if($opts['ip'])      $this->bind($opts['ip']);
        }

        function __destruct()
        {
            curl_close($this->_ch);
        }

        public function ua($ua)
        {
            return curl_setopt($this->_ch, CURLOPT_USERAGENT, $ua);
        }

        public function timeout($timeout)
        {
            return curl_setopt($this->_ch, CURLOPT_TIMEOUT, $timeout);
        }

        public function cookies($cookies)
        {
            return (curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $cookies) && curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $cookies));
        }

        public function redirect($redirectable)
        {
            return curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, $redirectable);
        }

        public function proxy($proxy)
        {
            return curl_setopt($this->_ch, CURLOPT_PROXY, $proxy);
        }

        public function bind($ip)
        {
            return curl_setopt($this->_ch, CURLOPT_INTERFACE, $ip);
        }

        public function referer($referer)
        {
            return curl_setopt($this->_ch, CURLOPT_REFERER, $referer);
        }

        public function get($url, $headers = false, $file = false, $spec_headers = false)
        {
            curl_setopt($this->_ch, CURLOPT_URL, $url);
            curl_setopt($this->_ch, CURLOPT_HEADER, $headers);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $spec_headers);
            }

            if($file)
            {
                if(!$fh = fopen($file, "w"))
                {
                    throw new WWWException("`$file': couldn't open for write");
                }
                curl_setopt($this->_ch, CURLOPT_FILE, $fh);
            }

            $page = curl_exec($this->_ch);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, array());
            }

            if($file)
            {
                fclose($fh);
                curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            }

            if($page === false)
            {
                throw new WWWException(curl_error($this->_ch));
            }

            return $page;
        }

        public function post($url, $post, $headers = false, $file = false, $spec_headers = false)
        {
            curl_setopt($this->_ch, CURLOPT_URL, $url);
            curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($this->_ch, CURLOPT_HEADER, $headers);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $spec_headers);
            }

            if($file)
            {
                if(!$fh = fopen($file, "w"))
                {
                    throw new WWWException("`$file': couldn't open for write");
                }
                curl_setopt($this->_ch, CURLOPT_FILE, $fh);
            }

            $page = curl_exec($this->_ch);

            if($spec_headers)
            {
                curl_setopt($this->_ch, CURLOPT_HTTPHEADER, array());
            }

            if($file)
            {
                fclose($fh);
                curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
            }

            if($page === false)
            {
                throw new WWWException(curl_error($this->_ch));
            }

            return $page;
        }
    }

    class WWWException extends Exception { }

    class UserAgent
    {
        static function random()
        {
            $agents = array
            (
                'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
                'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915',
                'Opera/9.63 (Windows NT 6.1; U; de) Presto/2.1.1',
                'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.3) Gecko/20070310 Iceweasel/2.0.0.3',
                'Opera/9.63 (Macintosh; Intel Mac OS X; U; de) Presto/2.1.1',
                'Opera/9.63 (X11; Linux i686; U; de) Presto/2.1.1',
                'Opera/9.50 (X11; Linux ppc; U; en)',
                'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.2)',
                'Mozilla/4.0 (Mozilla/4.0; MSIE 7.0; Windows NT 5.1; FDM; SV1)'
            );

            return $agents[array_rand($agents)];
        }
    }
?>



Спасибо , но я так и не смог этим воспользоваться.
В виду того что оно при запуске:

PHP Notice: Undefined index: ua in /sda1/test/script/my/WWW.class.php on line 11

Notice: Undefined index: ua in /sda1/test/script/my/WWW.class.php on line 11
PHP Notice: Undefined index: redirect in /sda1/test/script/my/WWW.class.php on line 12

Notice: Undefined index: redirect in /sda1/test/script/my/WWW.class.php on line 12
PHP Notice: Undefined index: timeout in /sda1/test/script/my/WWW.class.php on line 13

Notice: Undefined index: timeout in /sda1/test/script/my/WWW.class.php on line 13
PHP Notice: Undefined index: proxy in /sda1/test/script/my/WWW.class.php on line 15

Notice: Undefined index: proxy in /sda1/test/script/my/WWW.class.php on line 15
PHP Notice: Undefined index: ip in /sda1/test/script/my/WWW.class.php on line 16

Notice: Undefined index: ip in /sda1/test/script/my/WWW.class.php on line 16

Так что я пойду ковырять дальше свой вариант. Который по непонятным мне причинам перестал работать. 2 недели назад этот скрипт нормально отрабатывал.
Контакты для связи:

E-mail: ford1813@gmail.com

Telegram: @ford1813
Спасибо сказали:
pcodr
Сообщения: 283
ОС: Debian

Re: php, curl небольшой скрипт.

Сообщение pcodr »

Поставьте вначале error_reporting(E_ERROR); если вас смущают эти предупреждения, которые вовсе не ошибки.
Но даже с ними работает нормально, echo $page; думаю догадались сделать.
remote system type is unix
Спасибо сказали:
ford1813
Сообщения: 383
ОС: Slackware

Re: php, curl небольшой скрипт.

Сообщение ford1813 »

pcodr писал(а): ↑
06.12.2009 19:31
Поставьте вначале error_reporting(E_ERROR); если вас смущают эти предупреждения, которые вовсе не ошибки.
Но даже с ними работает нормально, echo $page; думаю догадались сделать.



Спасибо , предупреждения фиг с ними.

echo $page; естественно вставил в скрипт. Но я по прежнему получаю только

эту страничку
https://my.alloincognito.ru/clients/?sc=allo

Хотя в вашем скрипте и моём установлени параметр вида: $page = $www->get("https://my.alloincognito.ru/clients/?sc=allo&action=,go&interface=AUTO&prefix=495&login=8888888&password=1111");

Тут насколько я понимаю ситуация глубже. С оперы по ссылке https://my.alloincognito.ru/clients/?sc=all...p;password=1111 я уже не могу авторизоваться.
С firefox могу.

Скрипт мой перестал работать недели 2 назад. Скорей всего админ сайта зарезал curl запросы.
Контакты для связи:

E-mail: ford1813@gmail.com

Telegram: @ford1813
Спасибо сказали:
snikolaev
Сообщения: 195
ОС: Все подряд

Re: php, curl небольшой скрипт.

Сообщение snikolaev »

Хм, странно, я простой командой авторизовался. Вывел мой кабинет
curl https://my.alloincognito.ru/clients/?sc=all...p;password=1111

Код: Выделить всё

$ curl --version
curl 7.19.7 (i586-mandriva-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3 libidn/1.15 libssh2/1.2.1
Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp
Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz
Спасибо сказали: