How to fetch response from a callback url using php?

by aniya.jaskolski , in category: PHP General , 8 months ago

How to fetch response from a callback url using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 3 months ago

@aniya.jaskolski 

To fetch a response from a callback URL using PHP, you can use the cURL library. Here is an example code that demonstrates how to fetch the response from a callback URL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

// The callback URL you want to fetch the response from
$callbackUrl = 'http://example.com/callback';

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

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $callbackUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the cURL request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)){
    echo 'Error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Display the response
echo $response;

?>


In this code snippet, we first initialize a cURL session using curl_init() function. We then set the URL we want to fetch the response from using curl_setopt() function. We also set CURLOPT_RETURNTRANSFER option to true to return the response as a string.


After executing the cURL request using curl_exec() function, we check for any errors using curl_errno() and curl_error() functions. Finally, we close the cURL session with curl_close() and display the response fetched from the callback URL.