John Davidson

php - Transferring a search request from my website to another website and get response

0 comments
Message:


lughat_urdu this is a dictionary of Urdu language.
I'm making a website which take an input from the user and search the meaning of that word on this website server. If the meaning of that is found it return the meaning of the word, else return not available. Without redirect to the official website.
Below is my code which I try:


<form method="POST">
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Name</label>
<input type="text" name="name" class="form-control" placeholder="student name..." >
</div>
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</form>

<?php
if (isset($_POST['submit']))
{
$curl=curl_init('http://udb.gov.pk/');
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,'searchterm='.$_POST['name']);
$result=curl_exec($curl);
echo $result;
}
?>


My Answer:
php

// URL of the website to search
$url = 'http://www.example.com/search.php';

// Data to send in the request
$data = array(
'search' => $_POST['search']
);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Check if response is successful
if ($response) {
// Display the meaning of the word
echo $response;
} else {
// Display not available message
echo 'Meaning not available';
}

?>


This code uses cURL to send a POST request to the specified website with the search term provided by the user. It then retrieves the response from the website and displays it on the webpage. If the meaning of the word is found, it will be displayed, otherwise, a "Meaning not available" message will be shown.

Rate this post

4 of 5 based on 1699 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us