I have this JSON data:
{
userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',
itemKind: 0,
value: 1,
description: 'Saude',
itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa'
}
and I need to post to JSON URL:
http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount
How can I send this POST request using PHP?
My Answer:
You can send a JSON POST request using PHP by using the `curl` function. Here is an example code snippet to send the JSON data to the specified URL:
php
// JSON data
$data = array(
'userID' => 'a7664093-502e-4d2b-bf30-25a2b26d6021',
'itemKind' => 0,
'value' => 1,
'description' => 'Saude',
'itemID' => '03e76d0a-8bab-11e0-8250-000c29b481aa'
);
// URL to send the POST request
$url = 'http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount';
// Initialize cURL session
$ch = curl_init($url);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Set the POST request method
curl_setopt($ch, CURLOPT_POST, 1);
// Set the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Return the response instead of outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Print the response
echo $response;
?>
Make sure to replace the `$url` variable with the correct URL where you want to send the POST request.
Rate this post
5 of 5 based on 8058 votesComments