Tuesday, 19 February 2019

Google Charts


Quick Start

Here's a simple example of a page that displays a pie chart:

You can copy the snippet below to an .html file on your computer and open it in your browser to display the pie chart shown above:
<html>   <head>     <!--Load the AJAX API-->     <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>     <script type="text/javascript">       // Load the Visualization API and the corechart package.       google.charts.load('current', {'packages':['corechart']});       // Set a callback to run when the Google Visualization API is loaded.       google.charts.setOnLoadCallback(drawChart);       // Callback that creates and populates a data table,       // instantiates the pie chart, passes in the data and       // draws it.       function drawChart() {         // Create the data table.         var data = new google.visualization.DataTable();         data.addColumn('string', 'Topping');         data.addColumn('number', 'Slices');         data.addRows([           ['Mushrooms', 3],           ['Onions', 1],           ['Olives', 1],           ['Zucchini', 1],           ['Pepperoni', 2]         ]);         // Set chart options         var options = {'title':'How Much Pizza I Ate Last Night',                        'width':400,                        'height':300};         // Instantiate and draw our chart, passing in some options.         var chart = new google.visualization.PieChart(document.getElementById('chart_div'));         chart.draw(data, options);       }     </script>   </head>   <body>     <!--Div that will hold the pie chart-->     <div id="chart_div"></div>   </body> </html>

How About a Bar Chart?

Convert the pie chart to a bar chart by replacing
google.visualization.PieChart with
google.visualization.BarChart
in the code and reloading your browser. You may notice that the "Slices" legend is truncated. To fix this, change width to 500 from 400, save the file, and reload your browser.

Thursday, 21 December 2017

Analytics System Engineer

How to get an entry-level job as an Analytics System Engineer

04 Oct, 2016
As a fresh graduate with an interest in analytics, you can get an entry-level position as an Analytics Systems Engineer if you have some key skills to prototype, build, test, deploy and maintain analytics systems.
Python is the highest-ranked way to build analytics systems, with JavaScript for data visualization and user interfaces. There are other languages and technologies, of course, but the combination of Python and JavaScript gets you into a job, and then you can continue to learn while you earn.
This is how you can quickly acquire Python and JavaScript skills:
  1. Python & pandas
  2. JavaScript, AngularJS, HTML & CSS
If you acquire these skills, you can get a job in analytics.

Additional pointers: 
  1. Techniques and tools necessary to manipulate and analyze large datasets and summarize conclusions: https://www.springboard.com/learning-paths/data-analysis/
  2. Python & pandas
  3. JavaScript, AngularJS, HTML & CSS

Shaping Up With AngularJS

Saturday, 26 August 2017

PHP CURL POST & GET Examples – Submit Form using PHP CURL

http://hayageek.com/php-curl-post-get/

In PHP CURL POST tutorial, I have explained how to send HTTP GET / POST requests with PHP CURL library.
Why we need PHP CURL ?
To send HTTP GET requests, simply we can use file_get_contents() method.
1
file_get_contents('http://hayageek.com')
But sending POST request and handling errors are not easy with file_get_contents().

Sending HTTP requests is very simple with PHP CURL.You need to follow the four steps to send request.
step 1). Initialize CURL session
1
$ch = curl_init();
step 2). Provide options for the CURL session
1
2
3
curl_setopt($ch,CURLOPT_URL,"http://hayageek.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers
CURLOPT_URL -> URL to fetch
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.
For full list of options, check this PHP Documentation.
step 3). Execute the CURL session
1
$output=curl_exec($ch);
step 4). Close the session
1
curl_close($ch);
NoteYou can check whether CURL enabled/not with the following code.
1
2
3
4
5
6
7
if(is_callable('curl_init')){
   echo "Enabled";
}
else
{
   echo "Not enabled";
}

1.PHP CURL GET EXAMPLE

You can use the below code to send GET request.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function httpGet($url)
{
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//  curl_setopt($ch,CURLOPT_HEADER, false);
    $output=curl_exec($ch);
    curl_close($ch);
    return $output;
}
echo httpGet("http://hayageek.com");

2.PHP CURL POST EXAMPLE

PHP CURL POST & GET Examples
You can use the below code to submit form using PHP CURL.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function httpPost($url,$params)
{
  $postData = '';
   //create name value pairs seperated by &
   foreach($params as $k => $v)
   {
      $postData .= $k . '='.$v.'&';
   }
   $postData = rtrim($postData, '&');
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);   
    $output=curl_exec($ch);
    curl_close($ch);
    return $output;
}
How to use the function:
1
2
3
4
5
6
7
$params = array(
   "name" => "Ravishanker Kusuma",
   "age" => "32",
   "location" => "India"
);
echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);

3.SEND RANDOM USER-AGENT IN THE REQUESTS

You can use the below function to get Random User-Agent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function getRandomUserAgent()
{
    $userAgents=array(
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
        "Opera/9.20 (Windows NT 6.0; U; en)",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.50",
        "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1) Opera 7.02 [en]",
        "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.7) Gecko/20040624 Firefox/0.9",
        "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/48 (like Gecko) Safari/48"       
    );
    $random = rand(0,count($userAgents)-1);
    return $userAgents[$random];
}
Using CURLOPT_USERAGENT, you can set User-Agent string.
1
curl_setopt($ch,CURLOPT_USERAGENT,getRandomUserAgent());

4.HANDLE REDIRECTS (HTTP 301,302)

To handle URL redirects, set CURLOPT_FOLLOWLOCATION to TRUE.Maximum number of redirects can be controlled using CURLOPT_MAXREDIRS.
1
2
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_MAXREDIRS,2);//only 2 redirects

5.HOW TO HANDLE CURL ERRORS

we can use curl_errno(),curl_error() methods, to get the last errors for the current session.
curl_error($ch) -> returns error as string
curl_errno($ch) -> returns error number
You can use the below code to handle errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function httpGetWithErros($url)
{
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $output=curl_exec($ch);
    if($output === false)
    {
        echo "Error Number:".curl_errno($ch)."<br>";
        echo "Error String:".curl_error($ch);
    }
    curl_close($ch);
    return $output;
}
For the full list of errors, refer CURL errors