-
-
Save adscyber02/18f44ff52f3135a5ba8f6a42a71bd4e4 to your computer and use it in GitHub Desktop.
Mengirim dan menerima data JSON dengan PHP cURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//API URL | |
$url = 'http://www.exampleweb.com/api'; | |
//create a new cURL resource | |
$ch = curl_init($url); | |
//setup request to send json via POST | |
$data = array( | |
'username' => 'alltutorials', | |
'password' => 'password123456' | |
); | |
$payload = json_encode(array("user" => $data)); | |
//attach encoded JSON string to the POST fields | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); | |
//set the content type to application/json | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); | |
//return response instead of outputting | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
//execute the POST request | |
$result = curl_exec($ch); | |
//close cURL resource | |
curl_close($ch); | |
//Output response | |
echo "<pre>$result</pre>"; | |
//get response | |
$data = json_decode(file_get_contents('php://input'), true); | |
//output response | |
echo '<pre>'.$data.'</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kalau mengirim data nya gimana