Here it goes the first example. The system I need to scrap has a form of username and password to access data. So obviously, the first step is to log into the system by sending data using the HTTP POST method. I did this with these steps.
In your very simple HTTP browser with PHP put this little code in a public place, I put it under http://inside-out.xyz/examples/casperjs/form.php
<html>
<head> </head>
<body>
<?php
if ($_POST){
print $_POST[field1].'<br/>';
print $_POST[field2].'<br/>';
}
?>
<form name='form_example' method='POST'>
<input name="field1" type='text' />
<input name="field2" type='text'/>
<input type='submit' />
</form>
<body>
</html>
Put the following CasperJS code in your server (I have saved under form.js name):
var casper = require('casper').create();
var base64contents = null;
var contents = null;
casper.start('http://inside-out.xyz/examples/casperjs/form.php', function() {
base64contents = this.base64encode('http://inside-out.xyz/examples/casperjs/form.php', 'POST', {
field1: 'foo',
field2: 'bar'
});
contents = decode_base64(base64contents);
});
casper.run(function() {
this.echo(base64contents);
this.echo(contents).exit();
});
//decode_base64 from http://stackoverflow.com/questions/2820249/base64-
//encoding-and-decoding-in-client-side-javascript
function decode_base64(s) {
var e={},i,k,v=[],r='',w=String.fromCharCode;
var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
for(i=0;i<64;i++){e[v[i]]=i;}
for(i=0;i<s.length;i+=72){
var b=0,c,x,l=0,o=s.substring(i,i+72);
for(x=0;x<o.length;x++){
c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){r+=w((b>>>(l-=8))%256);}
}
}
return r;
}
Run the code by typing something like PHANTOMJS_EXECUTABLE=/usr/bin/phantomjs19 ~/casperjs-1.1-beta3/bin/casperjs form.js and here is my output.
PGh0bWw+CjxoZWFkPgo8L2hlYWQ+Cjxib2R5Pgpmb288YnIvPmJhcjxici8+PGZvcm0gbmFtZT0nZm9ybV9leGFtcGxlJyBtZXRob2Q9J1BPU1QnPgo8aW5wdXQgbmFtZT0iZmllbGQxIiB0eXBlPSd0ZXh0JyAvPgo8aW5wdXQgbmFtZT0iZmllbGQyIiB0eXBlPSd0ZXh0Jy8+CjxpbnB1dCB0eXBlPSdzdWJtaXQnIC8+CjwvZm9ybT4KPGJvZHk+CjwvaHRtbD4K
<html>
<head>
</head>
<body>
foo<br/>
bar<br/><form name='form_example' method='POST'>
<input name="field1" type='text' />
<input name="field2" type='text'/>
<input type='submit' />
</form>
<body>
</html>
Please note that URL's on both codes must match and the input tags as well.