Showing posts with label web scrap. Show all posts
Showing posts with label web scrap. Show all posts

Sunday, September 6, 2015

PHP login to another website with Goutte

I needed a break from all these frameworks and design patterns and code something which would solve me some immediate problem => bring immediate satisfaction.

Task: login to a website, access certain page, check if that page was updated, let me know via email.

After searching the web a little I found Goutte (https://github.com/FriendsOfPHP/Goutte), another library from Fabien Potencier.

My composer.json looks like this:

{
    "require": {
        "fabpot/goutte": "^3.1",
        "swiftmailer/swiftmailer": "@stable"
    }
}

1. Login to website and access certain page:


require_once __DIR__.'/vendor/autoload.php';
require_once 'private.php';  //I store users and passwords in an external file

use Goutte\Client;


// make a real request to an external site
$client = new Client();
$crawler = $client->request('GET', "http://www.testwebsite.com/login");

// select the form and fill in some values
$form = $crawler->selectButton('Login')->form();
$form['login-username'] = $odds_user;
$form['login-password'] = $odds_pass;

// submit that form
$crawler = $client->submit($form);
$crawler = $client->request('GET', "http://www.testwebsite.com/somepage");

//extract data 


$table_main = $crawler->filterXPath('//table[@class="some-class"]');

2. Check if page was updated and notify by email

2.1 First  configure SwiftMailer to work with your gmail account:

//---------------------------- Email configuration  -----------------------------------------
// Create the Transport
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername($email_user)
  ->setPassword($email_pass);


// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transporter);


2.2. Check if the size of the table filtered is the same as the previous size saved on disk in the file "previous_size.txt". If not then send email


$mail_content=$table_main->text();
$size=strlen($mail_content);

$old_value = file_get_contents('previous_size.txt');


//replace old value with new size and send me email
if ($old_value!=$size) {

    file_put_contents('previous_size.txt',$size);
   
    // Create the message
    $message = Swift_Message::newInstance()

          // Give the message a subject
          ->setSubject('New bet')

          // Set the From address with an associative array
          ->setFrom(array('youremail@gmail.com' => 'Script'))

          // Set the To addresses with an associative array
          ->setTo(array('personalemail@yahoo.com' => 'CPana'))

          // Give it a body
          ->setBody($mail_content)

          // And optionally an alternative body
          ->addPart('<q>' . $mail_content .'</q>', 'text/html')

          ;
   
    // Send the message
    $result = $mailer->send($message);
}