My Snippets

Authenticating and scrapping web page using PHP and cURL

PHP:
  1. <?php
  2.  
  3. define('POSTURL', 'http://yourdomain.com/Login.aspx'); // The POST destination
  4. define('POSTDATA', 'yourdatatopost'); // The data to log us in
  5.  
  6. $cURL = curl_init();
  7.  
  8. ### Let's login
  9. curl_setopt($cURL, CURLOPT_URL, POSTURL);            // Destination
  10. curl_setopt($cURL, CURLOPT_POST, 1);                // Please POST
  11. curl_setopt($cURL, CURLOPT_POSTFIELDS, POSTDATA);    // Data to POST
  12. curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 0);         // Do follow any redirect
  13. curl_setopt($cURL, CURLOPT_HEADER, 1);              // Get Headers
  14. curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);        // Get the output
  15. curl_setopt($cURL, CURLOPT_COOKIEJAR, 'cookie.txt');// Store cookies in cookie.txt
  16.  
  17. $loggedInOutput=curl_exec($cURL);
  18. echo '<hr/>'.$loggedInOutput.'<br/>';
  19.  
  20. ### Not let's scrape the page we need
  21. curl_setopt($cURL, CURLOPT_POST,0);                    // Do not POST
  22. curl_setopt($cURL, CURLOPT_POSTFIELDS, '');
  23. curl_setopt($cURL, CURLOPT_HEADER, 1);              // Get Headers
  24. curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);        // Get the output
  25. curl_setopt($cURL, CURLOPT_COOKIEFILE, 'cookie.txt'); // Read previously stored cookies
  26. curl_setopt($cURL, CURLOPT_URL, "http://yourdomain.com/realpagetoscrape.aspx");
  27.  
  28. $scrappeddata=curl_exec($cURL);
  29. echo '<hr/>'.$scrappeddata.'<br/>';
  30.  
  31. ### Now we can do whatever we need with that data
  32.  
  33. ### ...
  34. curl_close($cURL);
  35.  
  36. ?>

June 28th, 2008 Posted by | php | no comments
[]

Ruby Class Example

class MyClass <  MySuperClass

  def self.language
    return "English"
  end

  def initialize
     @my_inst_var = "Philippe"
     @@my_class_var = "Hello"
  end

  def hello
    return @@my_class_var + @my_inst_var
  end

end

June 14th, 2008 Posted by | ruby | no comments
[]