Skip to content

Instantly share code, notes, and snippets.

@tutweb
Created August 13, 2017 17:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tutweb/fa70ed013e30363ff333e09675846ca8 to your computer and use it in GitHub Desktop.
Save tutweb/fa70ed013e30363ff333e09675846ca8 to your computer and use it in GitHub Desktop.
Mengirim dan menerima data JSON dengan PHP cURL
<?php
//API URL
$url = 'http://www.contohweb.com/api';
//create a new cURL resource
$ch = curl_init($url);
//setup request to send json via POST
$data = array(
'username' => 'jurnalweb',
'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>';
@darojatun
Copy link

"user" ini dapatnya dari mana bg?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment