ugrás a tartalomhoz

Távoli url elérése HITELESÍTÉSSEL

mATTIAS · 2007. Ápr. 30. (H), 15.55
Sziasztok!

Régóta működnek weboldalaim, melyek más weboldalak tartalmaitól függenek, mindeddig nem volt szükség hitelesítésre, minden további nélkül el tudtam érni a távoli oldal forrását ezzel az osztályal (lejjebb)

Most azonban az lenne a feladatom, hogy a script magától bejelentkezzen az oldalra, majd cookie-val hitelesítse magát.

5letem szerint először megpingelné a távoli oldalt a megfeleő hitelesítő változókkal, ezután a szerver visszaad valamit, miszerint csináljon a böngésző (a script) egy cookie-t. Ezután a cookie-t mellékelve a HTTP HEADERBEN már mint bejelentkezett felhasználó kapnám a weblap forrását vissza.

Az első kérdés, hogy működhet-e az elképzelésem.

A második, hogy egy ilyen szerver-kliens komunikáció hogyan zajlik HTTP HEADER szinten, pl egy cookie-val való hitelesítés stb.

Előre is köszi a segítséget!



A most használt osztály:
  1. class HTTPRequest  
  2. {  
  3.    var $_fp;        // HTTP socket  
  4.    var $_url;        // full URL  
  5.    var $_host;        // HTTP host  
  6.    var $_protocol;    // protocol (HTTP/HTTPS)  
  7.    var $_uri;        // request URI  
  8.    var $_port;        // port  
  9.      
  10.    // scan url  
  11.    function _scan_url()  
  12.    {  
  13.        $req = $this->_url;  
  14.          
  15.        $pos = strpos($req'://');  
  16.        $this->_protocol = strtolower(substr($req, 0, $pos));  
  17.          
  18.        $req = substr($req$pos+3);  
  19.        $pos = strpos($req'/');  
  20.        if($pos === false)  
  21.            $pos = strlen($req);  
  22.        $host = substr($req, 0, $pos);  
  23.          
  24.        if(strpos($host':') !== false)  
  25.        {  
  26.            list($this->_host, $this->_port) = explode(':'$host);  
  27.        }  
  28.        else   
  29.        {  
  30.            $this->_host = $host;  
  31.            $this->_port = ($this->_protocol == 'https') ? 443 : 80;  
  32.        }  
  33.          
  34.        $this->_uri = substr($req$pos);  
  35.        if($this->_uri == '')  
  36.            $this->_uri = '/';  
  37.    }  
  38.      
  39.    // constructor  
  40.    function HTTPRequest($url)  
  41.    {  
  42.        $this->_url = $url;  
  43.        $this->_scan_url();  
  44.    }  
  45.      
  46.    // download URL to string  
  47.    function DownloadToString()  
  48.    {  
  49.        $crlf = "\r\n";  
  50.          
  51.        // generate request  
  52.        $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf  
  53.            .    'Host: ' . $this->_host . $crlf  
  54.            .    $crlf;  
  55.          
  56.        // fetch  
  57.        $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);  
  58.        fwrite($this->_fp, $req);  
  59.        while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))  
  60.            $response .= fread($this->_fp, 1024);  
  61.        fclose($this->_fp);  
  62.          
  63.        // split header and body  
  64.        $pos = strpos($response$crlf . $crlf);  
  65.        if($pos === false)  
  66.            return($response);  
  67.        $header = substr($response, 0, $pos);  
  68.        $body = substr($response$pos + 2 * strlen($crlf));  
  69.          
  70.        // parse headers  
  71.        $headers = array();  
  72.        $lines = explode($crlf$header);  
  73.        foreach($lines as $line)  
  74.            if(($pos = strpos($line':')) !== false)  
  75.                $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line$pos+1));  
  76.          
  77.        // redirection?  
  78.        if(isset($headers['location']))  
  79.        {  
  80.            $http = new HTTPRequest($headers['location']);  
  81.            return($http->DownloadToString($http));  
  82.        }  
  83.        else   
  84.        {  
  85.            return($body);  
  86.        }  
  87.    }  
  88. }  
 
1

Megvan!

mATTIAS · 2007. Május. 24. (Cs), 10.30
Megtaláltam a megoldást, illetve a könnyebbiket, curl használatával egész egyszerű a megoldás, megosztom nehogy valaki sokáig keresse, mint én!
  1. $ch = curl_init("http://site.com?valtozo_get1=ertek1&valtozo_get2=ertek2");  
  2.   
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  4. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; hu; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");  
  5. curl_setopt($ch, CURLOPT_HEADER, 1);  
  6. //curl_setopt($ch, CURLOPT_POST, 1);  
  7. //curl_setopt($ch, CURLOPT_POSTFIELDS, "valtozo_post1=ertek1&valtozo_post2=ertek2");  
  8. curl_setopt($ch, CURLOPT_COOKIE, "valtozo1=ertek1;valtozo2=ertek2");  
  9.   
  10. $eredmeny = curl_exec($ch);  
  11. curl_close($ch);