0% found this document useful (0 votes)
3 views

againPHP LAB

Uploaded by

www.mirfaraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

againPHP LAB

Uploaded by

www.mirfaraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

VIVEKANADA COLLEGE OF

TECHNOLOGY AND MANAGEMENT,


ALIGARH

PRATICAL FILE
FOR

WEB DEVELOPMENT USING PHP LAB

DIPLOMA 3RD YEAR (CS)


SESSION: 2023-24

SUBMITTED TO: SUBMITTED BY:


MR. PRABHAT SINGH NAME:
(CSE DEPTT.) ROLL NO:
Experiment No. 2
Object: Create Web forms and pages that properly use HTTP GET and
POST protocol as appropriate.

Creating a webform

<html>

<head>

<title>Registration Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<?php if (isset($_POST['form_submitted'])): ?>

<?php if (!isset($_POST['agree'])): ?>

<p>You have not accepted our terms of service</p>

<?php else: ?>

<h2>Thank You <?php echo $_POST['firstname']; ?></h2>

<p>You have been registered as

<?php echo $_POST['firstname'].'".$_POST['lastname']; ?>

</p>

<p> Go <a href="/registration_form2.php">back</a> to the form</p>

<?php endif; ?>

<?php else: ?>


[2:53 pm, 14/02/2024] Prabhat Sir: <h2>Registration Form</h2>

<form action="registration_form2.php" method="POST">

First name:

<input type="text" name="firstname">

<br> Last name:

<input type="text" name="lastname">

<br> Father's name:

<input type="text" name="father's name">

<br> Agree to Terms of Service:

<input type="checkbox" name="agree">

<br>

<input type="hidden" name="form_submitted" value="1" />

<input type="submit" value="Submit">

</form>

<?php endif; ?>

</body>

</html>

Result: Created a web form using HTTP GET and POST method.
Experiment No. 4
Object: Create PHP code that utilizes the commonly used API library functions built in
to PHP.

Theory: The full name of the API is Application Programming Interface. An API is a
set of

programming code that helps to communicate between two different software programs.
Many library

functions are used in the API, of which the most common library function is:

Requests: Requests are a library function that helps in issuing HttpRequests. Using the
Requests library it tells us what action we are going to perform by calling the API. A
total of four types of actions are performed in the Requests library is:

GET: This is the most common request method. Using this, we can get the data that we
want from the APL

POST: You can add new data to the server using this request method.

PUT: Using this request method, you can change the information present in the server.

DELETE: Using this request method, we can delete the information present in the
server which does not work in the server.

Programming:

Sheaders = array('Accept' => 'application/json');

Soptions = array('auth' => array('user', 'pass'));

Srequest = Requests::get('https://api.github.com/gists', Sheaders, Soptions);

var_dump($request->status_code); // int(200)

var_dump(Srequest->headers['content-type']); // string(31) "application/json;


charset=utf-8"

var_dump($request->body); // string(26891) "[...]"

Use this PHP code library GET POST, PUT and DELETE HTTP Request send.

Creating a PHP code:


<!DOCTYPE html>
<html>

<body>

<h1>Get hands on with JavaScript's Fetch API</h1>

<p>Write your requests in the script and watch the console and network logs.</p>

<script>

// GET retrieve all to-do's

fetch('https://jsonplaceholder.typicode.com/todos')

then(response => response.json())

then(json => console.log(json))

// will return all resources

// GET retrieves the to-do with specific URI (in this case id = 5)

fetch('https://jsonplaceholder.typicode.com/todos/5')

.then(response => response.json())

then(json => console.log(json))

/* will return this specific resource:

"userld": 1,

"id": 5,

"title": "Asian Publishers, Muzaffarnagar",

"completed": false

//POST adds a random id to the object sent

fetch('https://jsonplaceholder.typicode.com/todos', {
method: 'POST',

body: JSON.stringify({
Web Devlopement Using

userld: 1,

title: "clean room",

completed: false

}),

headers: {

"Content-type": "application/json; charset=UTF-8"

})

.then(response => response.json())

.then(json => console.log(json))

/* will return

"userld": 1,

"title": "clean room".

"completed": false,

"id": 201

// PUT to the resource with id = 5 to change the name of task

fetch('https://jsonplaceholder.typicode.com/todos/5', {

method: 'PUT',

body: JSON.stringify({
userld: 1,

id: 5,

title: "hello task",

completed: false

1), headers: {

"Content-type": "application/json; charset=UTF-8"

})

.then(response => response.json())

.then(json => console.log(ison))

/* will return

"userId": 1,

"id": 5,

"title": "hello task",

"completed": false

*/

// DELETE task with id = 1

fetch('https://jsonplaceholder.typicode.com/todos/1', {

method: 'DELETE'

})

// empty response: {}
</script>

</body>

</html>

Result: Thus created a first code that used the common library function requests.

You might also like