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);
Note: You 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

Thursday, 24 August 2017

how to enable and disable Button

--------HTML--------- 

<div id="butt1">
    <button id="btn11" onclick="check(this);">check1</button>


</div>

<div id="butt2">
    <button id="btn21" onclick="check23(this);" disabled hidden>check2</button>

</div>

----------java script-------

<script>


    var btnval = true;


    function check23(btnval) {

        document.getElementById("show2").innerHTML = "check 2: disable";
        document.getElementById("btn2").disabled = this.btnval;
        document.getElementById("btn2").hidden = this.btnval;
        document.getElementById("show4").innerText = "check 1:  enabel";
        document.getElementById("btn1").disabled = !this.btnval;
        document.getElementById("btn1").hidden = !this.btnval;

    }
    function check(btnval) {

            document.getElementById("show2").innerHTML = "check 2: enable";
            document.getElementById("btn2").disabled = !this.btnval;
        document.getElementById("btn2").hidden = !this.btnval;
            document.getElementById("show4").innerText = "check 1: disable";
            document.getElementById("btn1").disabled = this.btnval;
        document.getElementById("btn1").hidden = this.btnval;


    }
</script>

Monday, 27 February 2017

How to implement Cascading DropDownList with AngularJS and ASP.NET MVC

How to implement Cascading DropDownList with AngularJS and ASP.NET MVC



https://www.youtube.com/watch?v=Qd7V9beNb3Q

Wednesday, 18 January 2017

ASP. NET MVC

http://www.compilemode.com/2015/09/crud-operations-in-aspnet-mvc-5.html

https://code.msdn.microsoft.com/MVC5-Demo-with-Entity-c6bc81df


https://www.codeproject.com/articles/1032226/crud-operation-in-asp-net-mvc-and-angularjs

https://github.com/RajneeshVerma/CRUD-MVC4-AngularJS

https://www.codeproject.com/articles/1032226/crud-operation-in-asp-net-mvc-and-angularjs

Sunday, 8 January 2017

http://hmkcode.com/android-tutorial/

android-logo

Hello World!

Android Activity & Fragment

Android Broadcast

Android SQLite Database

Android User Interface

Android Layout & Style

Android Google Map v2

Android Google Massaging Service

Android Camera

Android Networking

Google Play Services

Android Studio

Spring

https://onedrive.live.com/?cid=9D50504E7116CC19&id=9D50504E7116CC19%21160&parId=9D50504E7116CC19%21107&action=locate

http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html


http://hmkcode.com/android-tutorial/