How To Get Data From Get Method Using Curl Code In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

Once we compiled PHP with cURL support, we can begin using the cURL functions. The basic idea behind the cURL functions is that we initialize a cURL session using the curl_init(), then we can set all our options for the transfer via the curl_setopt(), then we can execute the session with the curl_exec() and then we finish off our session using the curl_close().

get method curl code

First need to create html file
Doctype HTML file

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Get code</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
</body>
</html>


You need to create index file
Example.php
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Get code</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<style>
    body{
        background-color: #f3ecec;
    }
</style>
<body>
<div class="container">
    <div class="text-center mt-5">
        <h2>php Curl How to get code example</h2>
    </div>
    <br>
    <div class="col-md-12">
        <div class="row">
            <div class="col-md-2"></div>
            <div class="col-md-8">
                <div class="card bg-light p-5">
                    <h3>Example Loreum Ipsum</h3>
                    <p>Duis ut libero vel felis pellentesque tincidunt et eu sapien.
                        Pellentesque convallis nisl a pulvinar lobortis. Etiam sit
                        amet neque pulvinar, vulputate erat eget, fermentum ante.
                        Integer sit amet diam vel arcu facilisis tincidunt eu in tellus...
                    </p>
                </div>
            </div>
            <div class="col-md-2"></div>
        </div>
    </div>
</div>
</body>
</html>

You need to crate get method code
test.php
<?php
$post = [
    'name' => 'user',
    'password' => 'passuser',
    'gender'   => 1,
];

$ch = curl_init('http://localhost/curl_data/example.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>

 

Related Post