Learn Simple Rest Api In Curl With Example Using PHP

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

cURL stands for ‘Client URL Library’ and it allows you to connect and communicate with different types of servers with many different types of protocols HTTP, https, FTP, proxy, cookies.we can access any REST API with PHPs cURL Extension.

simple rest api using curl with php

First need to create HTML file
index.php

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

Using Curl method code
curl.php
<?php
$url="http://localhost/Curl/index.php";
$ch=curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
echo curl_exec($ch);
curl_close($ch);
?>

complate code Curl method with php
index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Rest Api</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Simple Rest Api with CURL In php</h1>
    </div>
    <br>
    <div class="well">
        <?php
        echo "In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content.";

        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post